Documentation
¶
Overview ¶
Package middleware provides HTTP middleware chaining for request processing.
Use this package to compose multiple middleware functions into a single handler chain. Each middleware in the chain can perform actions before and after calling the next handler, enabling cross-cutting concerns like logging, authentication, or request modification.
Middleware functions wrap the next handler and control whether to proceed with the chain by calling the next function. The CreateChain function assembles middleware in order, with the first middleware in the slice being the outermost wrapper.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreateChain ¶
func CreateChain(middlewares []Middleware, finalHandlerFunc http.HandlerFunc) http.HandlerFunc
CreateChain returns a http.HandlerFunc that invokes each middleware in order then the final http.HandlerFunc.
Types ¶
type Middleware ¶
type Middleware func(next http.HandlerFunc) http.HandlerFunc
Middleware functions are invoked each time the server handles a route. The chain of middleware is followed until the final handler is reached. The middleware must call next(w, r) if the middleware does not handle the request.
For example:
middleware := func(next http.HandlerFunc) http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
// Do middleware actions here.
// Calling next to invoke the next middleware or request handler.
next(writer, request)
}
}