From 108ba4d8c28b58e4a29bdf6505179ba994d74285 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lilian=20J=C3=B3nsd=C3=B3ttir?= Date: Wed, 24 Jan 2024 23:33:08 -0800 Subject: [PATCH] move middleware to its own file in anticipation of maybe adding more --- cmd/web/middleware.go | 12 ++++++++++++ cmd/web/routes.go | 5 +++-- 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 cmd/web/middleware.go diff --git a/cmd/web/middleware.go b/cmd/web/middleware.go new file mode 100644 index 0000000..da6d15b --- /dev/null +++ b/cmd/web/middleware.go @@ -0,0 +1,12 @@ +package main + +import ( + "net/http" + + "github.com/go-chi/chi/v5/middleware" +) + +// 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{ + middleware.Recoverer, +} diff --git a/cmd/web/routes.go b/cmd/web/routes.go index 7feb5d6..581fe08 100644 --- a/cmd/web/routes.go +++ b/cmd/web/routes.go @@ -4,7 +4,6 @@ import ( "net/http" "github.com/go-chi/chi/v5" - "github.com/go-chi/chi/v5/middleware" "git.burning.moe/celediel/burning.moe/internal/config" "git.burning.moe/celediel/burning.moe/internal/handlers" @@ -16,7 +15,9 @@ func routes(app *config.AppConfig) http.Handler { mux := chi.NewRouter() // Import some middleware - mux.Use(middleware.Recoverer) + for _, mw := range Middleware { + mux.Use(mw) + } // Setup static file server app.Logger.Debug("Setting up /static file server")