From 0de89e6bc6a467b2cc4261ae65464f40119cc0ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Herg=C3=A8s?= Date: Fri, 3 Oct 2025 18:40:45 +0200 Subject: feat(frontend): use htmx to dynamically navigate between pages --- backend/data.go | 13 +++++++++---- backend/home.go | 2 +- backend/logs.go | 3 ++- backend/router.go | 18 ++++++++++++++++-- backend/templates/base.html | 12 ++++++------ backend/templates/components.html | 25 +++++++++++++++++++++++++ backend/templates/home.html | 2 ++ backend/templates/home_log.html | 6 +++++- backend/templates/log.html | 2 +- backend/templates/logs_display.html | 25 ------------------------- backend/templates/simple.html | 6 +++++- frontend/index.ts | 26 ++++++++++++++++++++++++++ index.ts | 6 ------ package.json | 4 ++-- 14 files changed, 100 insertions(+), 50 deletions(-) create mode 100644 backend/templates/components.html delete mode 100644 backend/templates/logs_display.html create mode 100644 frontend/index.ts delete mode 100644 index.ts diff --git a/backend/data.go b/backend/data.go index 8922aca..cc61941 100644 --- a/backend/data.go +++ b/backend/data.go @@ -31,7 +31,7 @@ type data struct { } func (d *data) handleGeneric(w http.ResponseWriter, r *http.Request, name string, custom dataUsable) { - cfg := r.Context().Value("config").(*Config) + cfg := r.Context().Value(configKey).(*Config) if d.Domain == "" { d.Domain = cfg.Domain } @@ -75,15 +75,20 @@ func (d *data) handleGeneric(w http.ResponseWriter, r *http.Request, name string }, "next": func(i int) int { return i + 1 }, "before": func(i int) int { return i - 1 }, - }).ParseFS(templates, "templates/logs_display.html", fmt.Sprintf("templates/%s.html", name), "templates/base.html") + }).ParseFS(templates, "templates/components.html", fmt.Sprintf("templates/%s.html", name), "templates/base.html") if err != nil { panic(err) } + exec := "base.html" + if r.Context().Value(isUpdateKey).(bool) { + exec = "body" + w.Header().Set("Updated-Title", d.Title()) + } if custom == nil { - err = t.ExecuteTemplate(w, "base.html", d) + err = t.ExecuteTemplate(w, exec, d) } else { custom.SetData(d) - err = t.ExecuteTemplate(w, "base.html", custom) + err = t.ExecuteTemplate(w, exec, custom) } if err != nil { panic(err) diff --git a/backend/home.go b/backend/home.go index fd01318..b2a6dfa 100644 --- a/backend/home.go +++ b/backend/home.go @@ -66,7 +66,7 @@ func handleGenericRoot(w http.ResponseWriter, r *http.Request, name string) { if c, ok := rootContent[name]; ok { d.Content = c } else { - cfg := r.Context().Value("config").(*Config) + cfg := r.Context().Value(configKey).(*Config) path := filepath.Join(cfg.RootFolder, name+".md") b, err := os.ReadFile(path) if err != nil { diff --git a/backend/logs.go b/backend/logs.go index 2901559..c663ffb 100644 --- a/backend/logs.go +++ b/backend/logs.go @@ -124,11 +124,12 @@ func handleLogList(w http.ResponseWriter, r *http.Request) { if d == nil { return } + d.title = "logs" d.handleGeneric(w, r, "home_log", d) } func handleLog(w http.ResponseWriter, r *http.Request) { - cfg := r.Context().Value("config").(*Config) + cfg := r.Context().Value(configKey).(*Config) slug := chi.URLParam(r, "slug") path := filepath.Join(cfg.LogFolder, slug) d, ok := logs[path] diff --git a/backend/router.go b/backend/router.go index 33c685d..22c77a4 100644 --- a/backend/router.go +++ b/backend/router.go @@ -15,7 +15,11 @@ import ( "github.com/go-chi/httplog/v3" ) -const Version = "0.1.0" +const ( + Version = "0.1.0" + configKey = "config" + isUpdateKey = "is_update" +) //go:embed templates var templates embed.FS @@ -62,7 +66,17 @@ func NewRouter(debug bool, cfg *Config) *chi.Mux { })) r.Use(func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - ctx := context.WithValue(r.Context(), "config", cfg) + ctx := context.WithValue(r.Context(), configKey, cfg) + next.ServeHTTP(w, r.WithContext(ctx)) + }) + }) + r.Use(func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + val := false + if r.Header.Get("HX-Request") == "true" { + val = true + } + ctx := context.WithValue(r.Context(), isUpdateKey, val) next.ServeHTTP(w, r.WithContext(ctx)) }) }) diff --git a/backend/templates/base.html b/backend/templates/base.html index 68d3f8d..fec24ef 100644 --- a/backend/templates/base.html +++ b/backend/templates/base.html @@ -28,14 +28,14 @@
Logo
-{{ if .Article }} -
{{ template "body" . }}
-{{ else }} -
{{ template "body" . }}
-{{ end }} +{{ template "body" . }}