NextWks/src/core/ui/handler.go

47 lines
1.2 KiB
Go

package ui
import (
"net/http"
"path/filepath"
"git.lohmar.co.uk/lexton-it/NextWks/core/auth"
)
// Handler serves the workspace launcher UI.
type Handler struct {
appDir string
}
// NewHandler creates a UI handler that serves the launcher and static assets.
func NewHandler(appDir string) *Handler {
return &Handler{appDir: appDir}
}
// RegisterRoutes mounts the public UI routes on the given mux.
func (h *Handler) RegisterRoutes(mux *http.ServeMux, authGate func(http.Handler) http.Handler) {
staticDir := filepath.Join(h.appDir, "static")
mux.Handle("GET /static/", http.StripPrefix("/static", http.FileServer(http.Dir(staticDir))))
mux.Handle("GET /", authGate(http.HandlerFunc(h.launcherPage)))
}
func (h *Handler) launcherPage(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
userID, _ := auth.GetUserID(r)
role := getUserRole(r)
apps := DefaultApps(role)
component := LauncherPage(userID, role, apps)
component.Render(r.Context(), w)
}
// getUserRole extracts the user's role from the request context or session.
func getUserRole(r *http.Request) string {
if role, ok := r.Context().Value(auth.ContextRole).(string); ok {
return role
}
return "user"
}