Skip to content

Commit

Permalink
Update route groups
Browse files Browse the repository at this point in the history
  • Loading branch information
zivkovicmilos committed Aug 14, 2024
1 parent 88dfbea commit dce0fb8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 38 deletions.
22 changes: 13 additions & 9 deletions faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,22 @@ func NewFaucet(
f.mux.Use(corsMiddleware.Handler)
}

// Set up additional middlewares
for _, middleware := range f.middlewares {
f.mux.Use(middleware)
}

// Register the health check handler
f.mux.Get("/health", f.healthcheckHandler)

// Set up the request handlers
for _, handler := range f.handlers {
f.mux.Post(handler.Pattern, handler.HandlerFunc)
}
// Branch off another route group, so they don't influence
// "standard" routes like health
f.mux.Group(func(r chi.Router) {
// Apply user middlewares
for _, middleware := range f.middlewares {
r.Use(middleware)
}

// Apply standard and custom route handlers
for _, handler := range f.handlers {
r.Post(handler.Pattern, handler.HandlerFunc)
}
})

return f, nil
}
Expand Down
29 changes: 0 additions & 29 deletions faucet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,35 +69,6 @@ func TestFaucet_NewFaucet(t *testing.T) {
assert.NoError(t, err)
})

t.Run("with middlewares", func(t *testing.T) {
t.Parallel()

middlewares := []Middleware{
func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Example empty middleware
next.ServeHTTP(w, r)
})
},
}

cfg := config.DefaultConfig()
cfg.CORSConfig = nil // disable CORS middleware

f, err := NewFaucet(
&mockEstimator{},
&mockClient{},
WithConfig(cfg),
WithMiddlewares(middlewares),
)

require.NotNil(t, f)
assert.NoError(t, err)

// Make sure the middleware was set
assert.Len(t, f.mux.Middlewares(), len(middlewares))
})

t.Run("with handlers", func(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit dce0fb8

Please sign in to comment.