2024-01-11 20:07:47 -08:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
2024-01-13 18:33:26 -08:00
|
|
|
"html/template"
|
2024-01-11 20:07:47 -08:00
|
|
|
"net/http"
|
|
|
|
|
2024-01-13 18:33:26 -08:00
|
|
|
"bytes"
|
|
|
|
"context"
|
2024-01-12 15:48:31 -08:00
|
|
|
|
2024-01-13 18:33:26 -08:00
|
|
|
"github.com/a-h/templ"
|
2024-01-11 20:07:47 -08:00
|
|
|
"github.com/go-chi/chi/v5"
|
2024-01-12 15:48:31 -08:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
2024-01-11 20:07:47 -08:00
|
|
|
)
|
|
|
|
|
2024-01-13 18:33:26 -08:00
|
|
|
type TemplateState struct {
|
|
|
|
InnerHtml template.HTML
|
|
|
|
ActivePage string
|
2024-01-12 16:34:26 -08:00
|
|
|
}
|
|
|
|
|
2024-01-13 18:33:26 -08:00
|
|
|
const TemplateDir = "./web/templates/"
|
|
|
|
|
2024-01-12 15:48:31 -08:00
|
|
|
func SetLogLevel(level zerolog.Level) {
|
|
|
|
zerolog.SetGlobalLevel(level)
|
|
|
|
}
|
|
|
|
|
2024-01-11 20:07:47 -08:00
|
|
|
func WebRouter() http.Handler {
|
|
|
|
r := chi.NewRouter()
|
|
|
|
r.Get("/", getIndex)
|
2024-01-12 16:34:26 -08:00
|
|
|
r.Get("/hello", getHello)
|
2024-01-12 20:50:41 -08:00
|
|
|
r.Handle("/static/*", http.StripPrefix("/static/", http.FileServer(http.Dir("web/static/"))))
|
2024-01-11 20:07:47 -08:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2024-01-13 18:33:26 -08:00
|
|
|
//for name, values := range req.Header {
|
|
|
|
// Loop over all values for the name.
|
|
|
|
// for _, value := range values {
|
|
|
|
// log.Debug().Msg(name + " " + value);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
func renderFullPage(w http.ResponseWriter, c templ.Component, pageName string) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
// Render the provided templ component
|
|
|
|
err := c.Render(context.Background(), &buf)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("Fatal error reading hello template");
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the index template
|
2024-01-12 16:34:26 -08:00
|
|
|
index, err := template.ParseFiles(TemplateDir + "index.html")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal().
|
|
|
|
Err(err).
|
|
|
|
Msg("Fatal error reading index template")
|
|
|
|
}
|
2024-01-13 18:33:26 -08:00
|
|
|
|
|
|
|
// Inject the templ component html into the index template
|
|
|
|
err = index.Execute(w,
|
|
|
|
TemplateState{
|
|
|
|
InnerHtml: template.HTML(buf.String()),
|
|
|
|
ActivePage: pageName,
|
|
|
|
});
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("Fatal error reading hello template");
|
|
|
|
}
|
|
|
|
}
|
2024-01-12 16:34:26 -08:00
|
|
|
|
2024-01-13 18:33:26 -08:00
|
|
|
func getIndex(w http.ResponseWriter, req *http.Request) {
|
|
|
|
component := dashboard();
|
|
|
|
|
|
|
|
_, ok := req.Header["Hx-Request"]
|
|
|
|
if ok {
|
|
|
|
err := component.Render(context.Background(), w);
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("Couldn't render dashboard templ template");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
renderFullPage(w, component, "index");
|
|
|
|
}
|
2024-01-12 16:34:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func getHello(w http.ResponseWriter, req *http.Request) {
|
2024-01-12 15:48:31 -08:00
|
|
|
component := hello("Nick")
|
2024-01-13 18:33:26 -08:00
|
|
|
_, ok := req.Header["Hx-Request"]
|
|
|
|
if ok {
|
|
|
|
err := component.Render(context.Background(), w);
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("Couldn't render dashboard templ template");
|
|
|
|
}
|
|
|
|
}else {
|
|
|
|
renderFullPage(w, component, "hello");
|
|
|
|
}
|
2024-01-11 20:07:47 -08:00
|
|
|
}
|