Documentation
¶
Overview ¶
Package middleware provides stdlib net/http middleware for composing request processing pipelines without external web frameworks.
Package middleware provides composable HTTP middleware for Go services.
Each middleware follows the func(http.Handler) http.Handler pattern and can be chained using Chain(). Configuration structs expose a Default*Config() constructor with sensible development defaults.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetRequestID ¶
GetRequestID extracts the request ID from context.
Types ¶
type CORSConfig ¶
type CORSConfig struct {
// MaxAge is the duration (in seconds) browsers may cache preflight results.
MaxAge string
// AllowOrigins is the list of permitted origins. Use ["*"] to allow all.
AllowOrigins []string
// AllowMethods is the list of HTTP methods included in preflight responses.
AllowMethods []string
// AllowHeaders is the list of request headers the client may send.
AllowHeaders []string
// ExposedHeaders is the list of response headers a cross-origin browser is allowed to
// read, written as Access-Control-Expose-Headers. A response header not listed here is
// invisible to a `fetch` (e.g. the traceresponse trace id).
ExposedHeaders []string
}
CORSConfig holds Cross-Origin Resource Sharing settings. AllowOrigins of ["*"] permits all origins. For production, restrict to the specific frontend domain(s).
func DefaultCORSConfig ¶
func DefaultCORSConfig() CORSConfig
DefaultCORSConfig returns permissive CORS defaults suitable for development.
type Middleware ¶
Middleware is a function that wraps an http.Handler.
func CORS ¶
func CORS(cfg *CORSConfig) Middleware
CORS returns middleware that handles Cross-Origin Resource Sharing. It uses an O(1) set lookup for allowed origins and responds to preflight OPTIONS requests with 204 No Content. When AllowOrigins is ["*"], all origins are permitted (no Vary header).
func Chain ¶
func Chain(middlewares ...Middleware) Middleware
Chain composes multiple middleware into a single middleware. Middleware are applied in order: Chain(a, b, c)(handler) = a(b(c(handler))).
func Compression ¶
func Compression() Middleware
Compression returns middleware that gzip-compresses responses larger than 1KB. Each returned middleware owns one gzip writer pool shared by its requests.
func HTTPMetrics ¶
func HTTPMetrics(m interfaces.Metrics) Middleware
HTTPMetrics returns middleware that records RED-style HTTP server metrics on the shared metrics registry, using the OpenTelemetry-aligned metric names the Grafana dashboards query:
- http_server_requests_total{method,route,status}
- http_server_request_duration_seconds{method,route,status}
route is supplied by the caller (typically a low-cardinality matched pattern) to avoid unbounded label cardinality from raw request paths.
This middleware records metrics only. Per-request logging is emitted once at the Connect logging interceptor (for RPCs), so a request is not logged twice; health probes are not Connect RPCs and are therefore not logged at all.
func HTTPTracing ¶
func HTTPTracing() Middleware
HTTPTracing starts a SERVER span per HTTP request, propagating inbound trace context. This makes plain HTTP/REST traffic (not just Connect RPCs) part of the distributed trace and, crucially, puts a span in the request context so downstream logging (HTTPMetrics) can attach trace_id/span_id. Place it before HTTPMetrics in the chain. Sampling is governed by the global tracer provider.
func Recovery ¶
func Recovery(logger interfaces.Logger) Middleware
Recovery returns middleware that catches panics and returns 500 Internal Server Error.
func RequestID ¶
func RequestID() Middleware
RequestID returns middleware that ensures every request has an X-Request-ID header. If the incoming request doesn't have one, a new UUID is generated.