From 25461143ba734e6084602bd362d4fb6bfc49c752 Mon Sep 17 00:00:00 2001 From: Haelnorr Date: Sat, 24 Jan 2026 16:55:36 +1100 Subject: [PATCH] made htmx logging a flag that can be toggled --- .air.toml | 2 +- cmd/oslstats/httpserver.go | 12 ++++++------ cmd/oslstats/middleware.go | 18 ++++++++++++++++++ go.mod | 2 +- go.sum | 4 ++-- internal/config/flags.go | 3 +++ internal/view/layout/global.templ | 11 +++++++---- pkg/contexts/keys.go | 12 ++++++++++++ 8 files changed, 50 insertions(+), 14 deletions(-) diff --git a/.air.toml b/.air.toml index ad9f38b..ad5aa4c 100644 --- a/.air.toml +++ b/.air.toml @@ -3,7 +3,7 @@ testdata_dir = "testdata" tmp_dir = "tmp" [build] - args_bin = [] + args_bin = ["--htmxlog"] bin = "./tmp/main" cmd = "go build -o ./tmp/main ./cmd/oslstats" delay = 1000 diff --git a/cmd/oslstats/httpserver.go b/cmd/oslstats/httpserver.go index bd2763f..f13927b 100644 --- a/cmd/oslstats/httpserver.go +++ b/cmd/oslstats/httpserver.go @@ -17,7 +17,7 @@ import ( func setupHttpServer( staticFS *fs.FS, - config *config.Config, + cfg *config.Config, logger *hlog.Logger, bun *bun.DB, store *store.Store, @@ -27,7 +27,7 @@ func setupHttpServer( return nil, errors.New("No filesystem provided") } fs := http.FS(*staticFS) - httpServer, err := hws.NewServer(config.HWS) + httpServer, err := hws.NewServer(cfg.HWS) if err != nil { return nil, errors.Wrap(err, "hws.NewServer") } @@ -37,7 +37,7 @@ func setupHttpServer( "/static/favicon.ico", } - auth, err := setupAuth(config.HWSAuth, logger, bun, httpServer, ignoredPaths) + auth, err := setupAuth(cfg.HWSAuth, logger, bun, httpServer, ignoredPaths) if err != nil { return nil, errors.Wrap(err, "setupAuth") } @@ -57,14 +57,14 @@ func setupHttpServer( return nil, errors.Wrap(err, "httpServer.LoggerIgnorePaths") } - err = addRoutes(httpServer, &fs, config, bun, auth, store, discordAPI) + err = addRoutes(httpServer, &fs, cfg, bun, auth, store, discordAPI) if err != nil { return nil, errors.Wrap(err, "addRoutes") } - err = addMiddleware(httpServer, auth) + err = addMiddleware(httpServer, auth, cfg.Flags) if err != nil { - return nil, errors.Wrap(err, "httpServer.AddMiddleware") + return nil, errors.Wrap(err, "addMiddleware") } return httpServer, nil diff --git a/cmd/oslstats/middleware.go b/cmd/oslstats/middleware.go index cd4ea78..ff2c95c 100644 --- a/cmd/oslstats/middleware.go +++ b/cmd/oslstats/middleware.go @@ -1,9 +1,14 @@ package main import ( + "context" + "net/http" + "git.haelnorr.com/h/golib/hws" "git.haelnorr.com/h/golib/hwsauth" + "git.haelnorr.com/h/oslstats/internal/config" "git.haelnorr.com/h/oslstats/internal/db" + "git.haelnorr.com/h/oslstats/pkg/contexts" "github.com/pkg/errors" "github.com/uptrace/bun" @@ -12,13 +17,26 @@ import ( func addMiddleware( server *hws.Server, auth *hwsauth.Authenticator[*db.User, bun.Tx], + flags *config.Flags, ) error { err := server.AddMiddleware( auth.Authenticate(), + htmxLog(flags.HTMXLog), ) if err != nil { return errors.Wrap(err, "server.AddMiddleware") } return nil } + +func htmxLog(htmxlog bool) hws.Middleware { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ctx := context.WithValue(r.Context(), contexts.HTMXLogKey, htmxlog) + req := r.WithContext(ctx) + next.ServeHTTP(w, req) + }, + ) + } +} diff --git a/go.mod b/go.mod index e909fb8..efde9d7 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( git.haelnorr.com/h/golib/env v0.9.1 git.haelnorr.com/h/golib/ezconf v0.1.1 git.haelnorr.com/h/golib/hlog v0.10.4 - git.haelnorr.com/h/golib/hws v0.3.1 + git.haelnorr.com/h/golib/hws v0.3.2 git.haelnorr.com/h/golib/hwsauth v0.5.2 github.com/a-h/templ v0.3.977 github.com/joho/godotenv v1.5.1 diff --git a/go.sum b/go.sum index afdf0fa..f226f23 100644 --- a/go.sum +++ b/go.sum @@ -6,8 +6,8 @@ git.haelnorr.com/h/golib/ezconf v0.1.1 h1:4euTSDb9jvuQQkVq+x5gHoYPYyUZPWxoOSlWCI git.haelnorr.com/h/golib/ezconf v0.1.1/go.mod h1:rETDcjpcEyyeBgCiZSU617wc0XycwZSC5+IAOtXmwP8= git.haelnorr.com/h/golib/hlog v0.10.4 h1:vpCsV/OddjIYx8F48U66WxojjmhEbeLGQAOBG4ViSRQ= git.haelnorr.com/h/golib/hlog v0.10.4/go.mod h1:+wJ8vecQY/JITTXKmI3JfkHiUGyMs7N6wooj2wuWZbc= -git.haelnorr.com/h/golib/hws v0.3.1 h1:uFXAT8SuKs4VACBdrkmZ+dJjeBlSPgCKUPt8zGCcwrI= -git.haelnorr.com/h/golib/hws v0.3.1/go.mod h1:6ZlRKnt8YMpv5XcMXmyBGmD1/euvBo3d1azEvHJjOLo= +git.haelnorr.com/h/golib/hws v0.3.2 h1:OSwCwVxDerermyuoxrAYgt+i1zzwF/0Baoy8zmCxtuQ= +git.haelnorr.com/h/golib/hws v0.3.2/go.mod h1:6ZlRKnt8YMpv5XcMXmyBGmD1/euvBo3d1azEvHJjOLo= git.haelnorr.com/h/golib/hwsauth v0.5.2 h1:K4McXMEHtI5o4fAL3AZrmaMkwORNqSTV3MM6BExNKag= git.haelnorr.com/h/golib/hwsauth v0.5.2/go.mod h1:NOonrVU/lX8lzuV77eDEiTwBjn7RrzYVcSdXUJWeHmQ= git.haelnorr.com/h/golib/jwt v0.10.1 h1:1Adxt9H3Y4fWFvFjWpvg/vSFhbgCMDMxgiE3m7KvDMI= diff --git a/internal/config/flags.go b/internal/config/flags.go index c137b3e..ff42621 100644 --- a/internal/config/flags.go +++ b/internal/config/flags.go @@ -12,6 +12,7 @@ type Flags struct { ShowEnv bool GenEnv string EnvFile string + HTMXLog bool // Database reset (destructive) ResetDB bool @@ -33,6 +34,7 @@ func SetupFlags() (*Flags, error) { showEnv := flag.Bool("showenv", false, "Print all environment variable values and their documentation") genEnv := flag.String("genenv", "", "Generate a .env file with all environment variables (specify filename)") envfile := flag.String("envfile", ".env", "Specify a .env file to use for the configuration") + htmxlog := flag.Bool("htmxlog", false, "Run the server with all HTMX events logged") // Database reset (destructive) resetDB := flag.Bool("reset-db", false, "⚠️ DESTRUCTIVE: Drop and recreate all tables (dev only)") @@ -76,6 +78,7 @@ func SetupFlags() (*Flags, error) { ShowEnv: *showEnv, GenEnv: *genEnv, EnvFile: *envfile, + HTMXLog: *htmxlog, ResetDB: *resetDB, MigrateUp: *migrateUp, MigrateRollback: *migrateRollback, diff --git a/internal/view/layout/global.templ b/internal/view/layout/global.templ index 198483e..8a77c6f 100644 --- a/internal/view/layout/global.templ +++ b/internal/view/layout/global.templ @@ -3,10 +3,12 @@ package layout import "git.haelnorr.com/h/oslstats/internal/view/component/popup" import "git.haelnorr.com/h/oslstats/internal/view/component/nav" import "git.haelnorr.com/h/oslstats/internal/view/component/footer" +import "git.haelnorr.com/h/oslstats/pkg/contexts" // Global page layout. Includes HTML document settings, header tags // navbar and footer templ Global(title string) { + {{ htmxLogging := contexts.HTMXLog(ctx) }} - + if htmxLogging { + + }