Documentation
¶
Overview ¶
Package middleware provides composable HTTP middleware for use with net/http. It includes utilities for panic recovery, access logging, and middleware chaining.
Index ¶
- func AccessLogger(logger *slog.Logger, prefix string) func(h http.Handler) http.Handler
- func Queue(ms ...func(http.Handler) http.Handler) func(http.Handler) http.Handler
- func RecoverAndHandle(fallback http.Handler, logger *slog.Logger) func(h http.Handler) http.Handler
- func Stack(ms ...func(http.Handler) http.Handler) func(http.Handler) http.Handler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AccessLogger ¶
AccessLogger returns middleware that logs request and response details. If logger is nil, slog.Default() is used.
Fields logged include remote address, method, URL, protocol, response status, user agent, and request duration.
func Queue ¶
Queue composes a sequence of middleware in first-in-first-out (FIFO) order. The first middleware passed in will be the outermost, and the last will be closest to the final handler.
i.e. Queue(m1, m2)(handler) = m2(m1(handler)).
func RecoverAndHandle ¶
RecoverAndHandle returns middleware that recovers from panics in downstream handlers. If a panic occurs, it logs the error and stack trace using logger, then delegates to the fallback handler.
If logger is nil, slog.Default() is used.
Types ¶
This section is empty.