Documentation
¶
Overview ¶
Package middleware implements a simple middleware pattern for http handlers, along with implementations for some common middlewares.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Middleware ¶
A Middleware is a func that wraps an http.Handler.
func Chain ¶
func Chain(middlewares ...Middleware) Middleware
Chain creates a new Middleware that applies a sequence of Middlewares, so that they execute in the given order when handling an http request.
In other words, Chain(m1, m2)(handler) = m1(m2(handler))
A similar pattern is used in e.g. github.com/justinas/alice: https://github.com/justinas/alice/blob/ce87934/chain.go#L45
func Log ¶
func Log(logger *slog.Logger) Middleware
Log is a middleware that logs request start, end, duration, and status.
func Recover ¶
func Recover() Middleware
Recover is a middleware that recovers from panics in the delegate handler and prints a stack trace.
func RequestSize ¶
func RequestSize(n int64) Middleware
RequestSize limits the size of incoming request bodies.
func Timeout ¶
func Timeout(d time.Duration) Middleware
Timeout returns a new Middleware that times out each request after the given duration.