feat(layout): #49 update layout by navigation
This commit was merged in pull request #57.
This commit is contained in:
54
handler/root_and_404.go
Normal file
54
handler/root_and_404.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"spend-sparrow/handler/middleware"
|
||||
"spend-sparrow/service"
|
||||
"spend-sparrow/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.handleRootAnd404())
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user