Files
spend-sparrow/handler/root_and_404.go
Tim Wundenberg 328a9aa7e0
All checks were successful
Build Docker Image / Build-Docker-Image (push) Successful in 4m27s
feat(account): #49 fix unused imports
2025-05-08 23:03:40 +02:00

61 lines
1.2 KiB
Go

package handler
import (
"net/http"
"spend-sparrow/handler/middleware"
"spend-sparrow/service"
"spend-sparrow/template"
"github.com/a-h/templ"
)
type Index interface {
Handle(router *http.ServeMux)
}
type IndexImpl struct {
service service.Auth
render *Render
}
func NewIndex(service service.Auth, render *Render) Index {
return IndexImpl{
service: service,
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
}
}