Middlewares
httpx.Chain
composes middlewares around a handler. It takes any number of httpx.Middleware
and applies them in reverse order before executing the wrapped handler.
httpx.Middleware accepts both function types:
- func(http.Handler) http.Handler
- func(http.HandlerFunc) http.HandlerFunc
router.go
func (h *NoteHandler) CreateNote() http.HandlerFunc {
return httpx.Chain(
mw.ProvideUserContext
)(func(r *http.Request) (httpx.Response, error) {
// business logic
return httpx.JSON.Created(nil), nil
})
}
It is also convenient to have this located with your handler for testing because you are not forced to spin up the whole router to test a single handler.
You can add global middlewares in internal/provide/provide_http.go
like the logger one.
See the middlewares section for more details on available middlewares.
Last updated on