From 35557431da55ec3d8b8cff140a21fc98d6bc23ae Mon Sep 17 00:00:00 2001 From: Robin Olsen Date: Mon, 2 Mar 2026 23:09:25 +0100 Subject: [PATCH] add logging and metrics to web --- pkg/web/server/routes.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/pkg/web/server/routes.go b/pkg/web/server/routes.go index 11e90c5..5ed3637 100644 --- a/pkg/web/server/routes.go +++ b/pkg/web/server/routes.go @@ -9,6 +9,7 @@ import ( "strings" "time" + "github.com/LazyBachelor/LazyPM/internal/models" "github.com/LazyBachelor/LazyPM/pkg/web/handler" "github.com/NYTimes/gziphandler" "github.com/go-chi/chi/v5" @@ -26,6 +27,7 @@ func (s *Server) RegisterRoutes(assets embed.FS) http.Handler { r.Use(handler.HTMXMiddleware) r.Use(handler.AppMiddleware(s.App)) + r.Use(actionLoggingMiddleware(s.App)) s.handleAssets(r, assets) @@ -56,6 +58,35 @@ func (s *Server) RegisterRoutes(assets embed.FS) http.Handler { return gziphandler.GzipHandler(r) } +func actionLoggingMiddleware(app interface{ LogAction(string) }) func(http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if app != nil && shouldLogWebAction(r.URL.Path) { + app.LogAction(models.EncodeActionEvent(models.ActionEvent{ + Source: "web", + Action: "request", + Target: r.Method + " " + r.URL.Path, + Result: "ok", + })) + } + next.ServeHTTP(w, r) + }) + } +} + +func shouldLogWebAction(path string) bool { + if strings.HasPrefix(path, "/assets/") { + return false + } + + switch path { + case "/status": + return false + default: + return true + } +} + func (s *Server) handleAssets(r chi.Router, assets embed.FS) { var fileServer http.Handler