Documentation
¶
Overview ¶
Package httpmw provides utilities for composing HTTP middleware.
Instead of nesting middleware calls like:
mw1(mw2(mw3(handler)))
You can use Chain to compose them:
httpmw.NewChain(mw1, mw2, mw3).Then(handler)
The chain is reusable and can be extended:
base := httpmw.NewChain(logger, tracer) api := base.Append(auth, rateLimit) base.Then(publicHandler) api.Then(protectedHandler)
Works with http.ServeMux and any router that implements http.Handler.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Chain ¶
type Chain struct {
// contains filtered or unexported fields
}
Chain holds a sequence of middleware to be applied.
func NewChain ¶
func NewChain(middlewares ...Middleware) Chain
NewChain creates a new Chain with the given middleware. Middleware will be applied in the order they are provided, meaning the first middleware wraps the outermost layer.
Example:
httpmw.NewChain(mw1, mw2, mw3).Then(handler) // Equivalent to: mw1(mw2(mw3(handler))) // Request flow: mw1 -> mw2 -> mw3 -> handler
func (Chain) Append ¶
func (c Chain) Append(middlewares ...Middleware) Chain
Append extends the chain with additional middleware, returning a new Chain. The original chain is not modified.
Example:
base := httpmw.NewChain(logger, tracer) api := base.Append(auth) // base remains: logger -> tracer // api becomes: logger -> tracer -> auth
func (Chain) Handler ¶
func (c Chain) Handler() Middleware
Handler returns a middleware function that applies the entire chain. This allows a Chain to be used as a single middleware in another chain or with routers that use the Use() pattern.
Example:
common := httpmw.NewChain(logger, tracer)
mux := http.NewServeMux()
// Use as middleware with a mux wrapper
http.Handle("/", common.Handler()(mux))
// Or combine chains
full := httpmw.NewChain(common.Handler(), auth)
func (Chain) Then ¶
Then applies the chain to the final handler and returns the resulting http.Handler. This is the terminal operation that produces a usable handler.
Example:
handler := httpmw.NewChain(logger, auth).Then(myHandler)
http.Handle("/api", handler)
func (Chain) ThenFunc ¶
func (c Chain) ThenFunc(fn http.HandlerFunc) http.Handler
ThenFunc applies the chain to an http.HandlerFunc. This is a convenience method equivalent to Then(http.HandlerFunc(fn)).
Example:
handler := httpmw.NewChain(logger, auth).ThenFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello"))
})
func (Chain) Wrap ¶
Wrap applies the chain to a handler and returns it. This is an alias for Then, provided for semantic clarity when wrapping mux/routers.
Example:
mux := http.NewServeMux()
mux.HandleFunc("/", homeHandler)
// Wrap the entire mux with middleware
http.ListenAndServe(":8080", httpmw.NewChain(logger, recovery).Wrap(mux))
Directories
¶
| Path | Synopsis |
|---|---|
|
Package bodylimit provides HTTP middleware that limits the size of request bodies.
|
Package bodylimit provides HTTP middleware that limits the size of request bodies. |
|
Package logger provides HTTP request logging middleware for Go servers.
|
Package logger provides HTTP request logging middleware for Go servers. |
|
Package ratelimit provides HTTP middleware for rate limiting requests.
|
Package ratelimit provides HTTP middleware for rate limiting requests. |
|
Package recovery provides HTTP middleware that recovers from panics.
|
Package recovery provides HTTP middleware that recovers from panics. |
|
Package tracer provides HTTP middleware for request tracing with unique IDs.
|
Package tracer provides HTTP middleware for request tracing with unique IDs. |