made htmx logging a flag that can be toggled

This commit is contained in:
2026-01-24 16:55:36 +11:00
parent 818f107143
commit 25461143ba
8 changed files with 50 additions and 14 deletions

View File

@@ -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

View File

@@ -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)
},
)
}
}