This repository has been archived on 2025-08-09. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
web-app-template/handler/index_and_404.go
Tim Wundenberg 52f6d3d706
All checks were successful
Build Docker Image / Build-Docker-Image (push) Successful in 47s
chore: #174 make into template
2024-12-31 12:25:30 +01:00

51 lines
943 B
Go

package handler
import (
"web-app-template/handler/middleware"
"web-app-template/service"
"web-app-template/template"
"net/http"
"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.handleIndexAnd404())
}
func (handler IndexImpl) handleIndexAnd404() 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 {
comp = template.Index()
status = http.StatusOK
}
handler.render.RenderLayoutWithStatus(r, w, comp, user, status)
}
}