Skip to content

Commit

Permalink
fix: cleaned off middleware bloat (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
noxecane authored Apr 18, 2024
1 parent 947d7a3 commit 32b66f0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
15 changes: 2 additions & 13 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,9 @@ jobs:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: 1.21

- name: Restore cache
uses: actions/cache@v3
with:
path: |
~/go/pkg/mod
~/.cache/go-build
vendor
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
go-version: 1.22.x

- name: Build and test
run: |
Expand Down
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 32b66f0

Please sign in to comment.