Skip to content

Commit

Permalink
fix: cleaned off middleware bloat
Browse files Browse the repository at this point in the history
  • Loading branch information
noxecane committed Apr 18, 2024
1 parent 947d7a3 commit d09f388
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand Down
29 changes: 19 additions & 10 deletions webpack/webpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand All @@ -40,8 +40,6 @@ type WebpackOpts struct {
//
// - Request Logging
//
// - Response time header
//
// - Panic Recovery(with special support for api.Error)
//
// - Timeouts on request context
Expand Down Expand Up @@ -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
}
Expand All @@ -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))
}

0 comments on commit d09f388

Please sign in to comment.