Documentation
¶
Overview ¶
Package middleware provides composable net/http middleware following the standard func(http.Handler) http.Handler pattern.
Index ¶
- Constants
- Variables
- func CORSMiddleware(opts CORSOptions) func(http.Handler) http.Handler
- func Chain(mw ...func(http.Handler) http.Handler) func(http.Handler) http.Handler
- func CorrelationIDMiddleware(log *slog.Logger) func(http.Handler) http.Handler
- func GetCorrelationID(r *http.Request) string
- func GetLogger(r *http.Request, fallback *slog.Logger) *slog.Logger
- func LoggingMiddleware(fallback *slog.Logger) func(http.Handler) http.Handler
- func RecoveryMiddleware(fallback *slog.Logger) func(http.Handler) http.Handler
- func WithCORS(pattern string, h http.HandlerFunc, opts CORSOptions) (string, http.HandlerFunc)
- type CORSOptions
Constants ¶
const CorrelationIDHeader = "X-Correlation-ID"
CorrelationIDHeader is the canonical header name for the correlation / trace ID.
Variables ¶
var DefaultAllowedHeaders = []string{"Content-Type", "Authorization", "X-Correlation-ID"}
DefaultAllowedHeaders are the headers permitted by default when building CORSOptions.
Functions ¶
func CORSMiddleware ¶
func CORSMiddleware(opts CORSOptions) func(http.Handler) http.Handler
CORSMiddleware applies CORS headers globally to every route handled by the wrapped handler.
func Chain ¶
Chain composes middleware left-to-right so that the first argument is the outermost wrapper (executed first on a request, last on a response).
Chain(A, B, C)(handler) == A(B(C(handler)))
func CorrelationIDMiddleware ¶
CorrelationIDMiddleware reads the X-Correlation-ID request header (or generates a new UUID v4 if absent), stores it and a correlation-aware *slog.Logger in the request context, and echoes the ID in the response header.
Register this as the outermost middleware so all subsequent handlers have access to a correlation-aware logger.
func GetCorrelationID ¶
GetCorrelationID returns the correlation ID stored by CorrelationIDMiddleware. Returns an empty string when the middleware has not run.
func GetLogger ¶
GetLogger returns the correlation-aware logger stored by CorrelationIDMiddleware. Falls back to fallback when the middleware has not run (e.g. in tests).
func LoggingMiddleware ¶
LoggingMiddleware logs each request (method, path, status, latency) via slog after the downstream handler completes. It uses the correlation-aware logger stored by CorrelationIDMiddleware when available.
Must be registered after CorrelationIDMiddleware to capture correlation IDs.
func RecoveryMiddleware ¶
RecoveryMiddleware catches panics from downstream handlers, logs the stack trace, and writes a 500 JSON response so the server stays alive.
func WithCORS ¶
func WithCORS(pattern string, h http.HandlerFunc, opts CORSOptions) (string, http.HandlerFunc)
WithCORS wraps handler with per-route CORS and returns the (pattern, wrappedHandler) pair so it can be spread directly into mux.HandleFunc:
mux.HandleFunc(middleware.WithCORS("GET /users/{id}", handler, opts))
Types ¶
type CORSOptions ¶
type CORSOptions struct {
// AllowedOrigins lists origins that may access the resource.
// Empty (the default) denies all cross-origin access — fail closed.
// Use ["*"] to allow any origin (cannot be combined with AllowCredentials).
AllowedOrigins []string
// AllowedMethods lists the HTTP methods permitted for the resource.
AllowedMethods []string
// AllowedHeaders lists the request headers the browser may send.
AllowedHeaders []string
// AllowCredentials indicates whether cookies / auth headers are allowed.
AllowCredentials bool
// MaxAge is the number of seconds the preflight response may be cached.
MaxAge int
}
CORSOptions configures CORS behaviour for CORSMiddleware and WithCORS.