From bedf6cb8e9fd405c0f0a70be571a4d5fc9d299e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lilian=20J=C3=B3nsd=C3=B3ttir?= Date: Wed, 24 Jan 2024 23:36:29 -0800 Subject: [PATCH] actual request logger middleware instead of log calls in every handler --- cmd/web/main.go | 5 ++++- cmd/web/middleware.go | 6 ++++++ internal/config/config.go | 2 ++ internal/handlers/handlers.go | 4 ---- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/cmd/web/main.go b/cmd/web/main.go index 83d466b..6f7a77f 100644 --- a/cmd/web/main.go +++ b/cmd/web/main.go @@ -11,9 +11,12 @@ import ( "git.burning.moe/celediel/burning.moe/internal/render" ) +// App wide config data and such +var app config.AppConfig + func main() { // Initialise app and config - app := config.Initialise() + app = config.Initialise() // Initialise handlers and renderer handlers.Initialise(&app) diff --git a/cmd/web/middleware.go b/cmd/web/middleware.go index da6d15b..1d122e5 100644 --- a/cmd/web/middleware.go +++ b/cmd/web/middleware.go @@ -8,5 +8,11 @@ import ( // Middleware is a slice of Middleware (aka func(n http.Handler) http.Handler {}) var Middleware []func(next http.Handler) http.Handler = []func(next http.Handler) http.Handler{ + func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + app.Logger.Info("REQUEST", "url", r.URL, "ip", r.RemoteAddr, "useragent", r.UserAgent()) + next.ServeHTTP(w, r) + }) + }, middleware.Recoverer, } diff --git a/internal/config/config.go b/internal/config/config.go index dde3d8d..85cc790 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -36,6 +36,8 @@ type ConfigDatabase struct { // Initialises the app wide AppConfig, loads values from environment, and set up the Logger func Initialise() AppConfig { app := *defaults + + // Setup logger app.Logger = log.NewWithOptions(os.Stderr, log.Options{ ReportTimestamp: true, TimeFormat: time.TimeOnly, diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index 047b6cb..aa3d187 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -44,8 +44,6 @@ func Initialise(a *config.AppConfig) { // HomeHandler handles /, generating data from Handlers func HomeHandler(w http.ResponseWriter, r *http.Request) { - app.Logger.Info("Got request for homepage") - page := "home.page.tmpl" d := models.TemplateData{} @@ -80,7 +78,6 @@ func HomeHandler(w http.ResponseWriter, r *http.Request) { // makeBasicHandler returns a simple handler that renders a template from `name`.page.tmpl func makeBasicHandler(name string) func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) { - app.Logger.Infof("Got request for %s page", name) pageName := name + ".page.tmpl" render.RenderTemplate(w, pageName) } @@ -95,7 +92,6 @@ func makeLinksHandler(name string) func(w http.ResponseWriter, r *http.Request) app.Logger.Error(fmt.Sprintf("couldn't get %s from cache", page), "err", err) } - app.Logger.Infof("Got request for %s links page", name) data, err := td.LoadTemplateData(name) if err != nil { app.Logger.Fatal("couldn't load template data for "+name, "err", err)