Files
spend-sparrow/internal/handler/root_and_404.go
Tim Wundenberg 75433834ed
All checks were successful
Build Docker Image / Build-Docker-Image (push) Successful in 1m7s
Build and Push Docker Image / Build-And-Push-Docker-Image (push) Successful in 1m10s
feat: extract authentication to domain package
2025-12-25 07:39:48 +01:00

72 lines
1.3 KiB
Go

package handler
import (
"net/http"
"spend-sparrow/internal/core"
"spend-sparrow/internal/template"
"spend-sparrow/internal/utils"
"github.com/a-h/templ"
)
type Index interface {
Handle(router *http.ServeMux)
}
type IndexImpl struct {
r *core.Render
c core.Clock
}
func NewIndex(r *core.Render, c core.Clock) Index {
return IndexImpl{
r: r,
c: c,
}
}
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) {
core.UpdateSpan(r)
user := core.GetUser(r)
htmx := utils.IsHtmx(r)
var comp templ.Component
var status int
if r.URL.Path != "/" {
comp = template.NotFound()
status = http.StatusNotFound
} else {
if user != nil {
utils.DoRedirect(w, r, "/dashboard")
return
} else {
comp = template.Index()
}
status = http.StatusOK
}
if htmx {
handler.r.RenderWithStatus(r, w, comp, status)
} else {
handler.r.RenderLayoutWithStatus(r, w, comp, user, status)
}
}
}
func (handler IndexImpl) handleEmpty() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
core.UpdateSpan(r)
// Return nothing
}
}