37 lines
896 B
Go
37 lines
896 B
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"time"
|
|
|
|
"git.haelnorr.com/h/golib/hws"
|
|
"git.haelnorr.com/h/oslstats/internal/db"
|
|
"git.haelnorr.com/h/oslstats/internal/view/page"
|
|
"github.com/pkg/errors"
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
func AdminDashboard(s *hws.Server, conn *bun.DB) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second)
|
|
defer cancel()
|
|
|
|
tx, err := conn.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
throwInternalServiceError(s, w, r, "Database error", errors.Wrap(err, "conn.BeginTx"))
|
|
return
|
|
}
|
|
defer func() { _ = tx.Rollback() }()
|
|
|
|
users, err := db.GetUsers(ctx, tx, nil)
|
|
if err != nil {
|
|
throwInternalServiceError(s, w, r, "Database error", errors.Wrap(err, "db.GetUsers"))
|
|
return
|
|
}
|
|
_ = tx.Commit()
|
|
|
|
renderSafely(page.AdminDashboard(users), s, r, w)
|
|
})
|
|
}
|