From d09f388bc388817ade3906c933bc9731a57ed441 Mon Sep 17 00:00:00 2001 From: Olakunle Arewa Date: Thu, 18 Apr 2024 01:16:31 +0100 Subject: [PATCH] fix: cleaned off middleware bloat --- go.mod | 2 +- webpack/webpack.go | 29 +++++++++++++++++++---------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/go.mod b/go.mod index a329741..5371476 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/noxecane/anansi -go 1.21 +go 1.22 require ( github.com/go-chi/chi/v5 v5.0.10 diff --git a/webpack/webpack.go b/webpack/webpack.go index 00d881f..e62bc51 100644 --- a/webpack/webpack.go +++ b/webpack/webpack.go @@ -28,7 +28,7 @@ type WebpackOpts struct { // // - Automatic Request IDs // -// - Response time middleware(and metrics if a registry is passed) +// - Response time middleware and header(and metrics if a registry is passed) // // - Real IP middleware // @@ -40,8 +40,6 @@ type WebpackOpts struct { // // - Request Logging // -// - Response time header -// // - Panic Recovery(with special support for api.Error) // // - Timeouts on request context @@ -75,11 +73,25 @@ func Webpack(router *chi.Mux, log zerolog.Logger, conf WebpackOpts) { router.Use(api.Recoverer(conf.Environment)) } +// HTMLPack sets a reasonable set of middleware in the right order taking into consideration +// those that defer computation(especially) +// +// The middleware set up includes: +// +// - Automatic Request IDs +// +// - Real IP middleware +// +// - Compressing response body +// +// - Request Logging +// +// - Response time header +// +// - Panic Recovery(with special support for api.Error) +// +// - Timeouts on request context func HTMLpack(router *chi.Mux, log zerolog.Logger, conf WebpackOpts) { - if len(conf.CORSOrigins) > 0 { - requests.CORS(conf.Environment, conf.CORSOrigins...) - } - if conf.CompressionLevel == 0 { conf.CompressionLevel = 5 } @@ -97,8 +109,5 @@ func HTMLpack(router *chi.Mux, log zerolog.Logger, conf WebpackOpts) { router.Use(requests.Timeout(conf.Timeout)) router.Use(responses.ResponseTime) - if conf.Registry != nil { - router.Use(responses.RequestDuration(conf.Registry)) - } router.Use(api.Recoverer(conf.Environment)) }