package dashboard import ( "net/http" "github.com/xlgmokha/x/pkg/log" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/cfg" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/middleware" "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/views" ) type Controller struct { } func New() *Controller { return &Controller{} } func (c *Controller) MountTo(mux *http.ServeMux) { requireUser := middleware.RequireUser() mux.Handle("GET /dashboard", requireUser(http.HandlerFunc(c.Show))) mux.Handle("GET /dashboard/nav", http.HandlerFunc(c.Navigation)) } func (c *Controller) Show(w http.ResponseWriter, r *http.Request) { currentUser := cfg.CurrentUser.From(r.Context()) w.WriteHeader(http.StatusOK) w.Header().Add("Content-Type", "text/html") dto := &ViewDashboardDTO{CurrentUser: currentUser} if err := views.Render(w, "dashboard/show", dto); err != nil { log.WithFields(r.Context(), log.Fields{"error": err}) w.WriteHeader(http.StatusInternalServerError) return } } func (c *Controller) Navigation(w http.ResponseWriter, r *http.Request) { currentUser := cfg.CurrentUser.From(r.Context()) w.WriteHeader(http.StatusOK) w.Header().Add("Content-Type", "text/html") dto := &NavigationDTO{ CurrentUser: currentUser, IsLoggedIn: currentUser != nil, } if err := views.Render(w, "dashboard/nav", dto); err != nil { log.WithFields(r.Context(), log.Fields{"error": err}) w.WriteHeader(http.StatusInternalServerError) return } }