Files
comicverse/handlers/pages/dashboard.templ
Gustavo L de Mello (Guz) 218b991caa refactor(lib): move app routing to lib package
These packages and functions under lib will one day be part of it's
dedicated library/framework, so separating them here helps for the
future.
2024-10-23 19:06:03 -03:00

58 lines
1.2 KiB
Plaintext

package pages
import (
"net/http"
"errors"
"log"
"forge.capytal.company/capytalcode/project-comicverse/lib/router/rerrors"
"forge.capytal.company/capytalcode/project-comicverse/lib/cookies"
"forge.capytal.company/capytalcode/project-comicverse/templates/layouts"
)
type Dashboard struct{}
type DashboardCookie struct {
Hello string `cookie:"dashboard-cookie"`
Bool bool
Test int
}
func (p *Dashboard) ServeHTTP(w http.ResponseWriter, r *http.Request) {
hasCookie := true
var c DashboardCookie
if err := cookies.UnmarshalRequest(r, &c); errors.Is(err, cookies.ErrNoCookie) {
hasCookie = false
c = DashboardCookie{Hello: "Hello world", Bool: true, Test: 69420}
} else if err != nil {
rerrors.InternalError(err).ServeHTTP(w, r)
return
} else {
hasCookie = true
}
log.Print(hasCookie, c)
if ck, err := cookies.Marshal(c); err != nil {
rerrors.InternalError(err).ServeHTTP(w, r)
} else {
http.SetCookie(w, ck)
}
if err := p.Component().Render(r.Context(), w); err != nil {
rerrors.InternalError(err).ServeHTTP(w, r)
return
}
}
templ (p *Dashboard) Component() {
@layouts.Page() {
<div class="text-danger-100 font-sans">
<p>Hello world</p>
<p>test</p>
<p>test</p>
</div>
}
}