package handler import ( "net/http" "spend-sparrow/internal/handler/middleware" "spend-sparrow/internal/template" "github.com/a-h/templ" ) type Index interface { Handle(router *http.ServeMux) } type IndexImpl struct { render *Render } func NewIndex(render *Render) Index { return IndexImpl{ render: render, } } func (handler IndexImpl) Handle(router *http.ServeMux) { router.Handle("/", handler.handleRootAnd404()) router.Handle("/empty", handler.handleEmpty()) } func (handler IndexImpl) handleRootAnd404() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { user := middleware.GetUser(r) var comp templ.Component var status int if r.URL.Path != "/" { comp = template.NotFound() status = http.StatusNotFound } else { if user != nil { comp = template.Dashboard() } else { comp = template.Index() } status = http.StatusOK } handler.render.RenderLayoutWithStatus(r, w, comp, user, status) } } func (handler IndexImpl) handleEmpty() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { // Return nothing } }