Documentation
¶
Overview ¶
Package middleware provides HTTP middleware for go-httpkit.
Metrics records request count and duration (Prometheus); optional Registerer and PathFromRequest. Recoverer recovers panics and responds with 500; pass a go-logkit Logger for stack logging. Timeout cancels the request context after a duration and responds with 503 on timeout. SecurityHeaders sets common security response headers. RequestID sets or propagates X-Request-ID and stores it in context; use GetRequestID to read it.
Index ¶
- func GetRequestID(ctx context.Context) string
- func Metrics(reg prometheus.Registerer, pathFromRequest PathFromRequest) func(http.Handler) http.Handler
- func Recoverer(log logger.Logger) func(http.Handler) http.Handler
- func RequestID() func(http.Handler) http.Handler
- func SecurityHeaders() func(http.Handler) http.Handler
- func Timeout(d time.Duration) func(http.Handler) http.Handler
- type PathFromRequest
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetRequestID ¶ added in v0.1.3
GetRequestID returns the request ID from the context, or "" if not set.
func Metrics ¶
func Metrics(reg prometheus.Registerer, pathFromRequest PathFromRequest) func(http.Handler) http.Handler
Metrics returns middleware that records request count and duration. reg can be nil to use prometheus.DefaultRegisterer. pathFromRequest can be nil.
func Recoverer ¶ added in v0.1.3
Recoverer recovers panics, logs the panic and stack trace with log (if non-nil), and responds with 500 JSON.
func RequestID ¶ added in v0.1.3
RequestID middleware sets X-Request-ID from the request header or generates a new UUID, and stores it in the context.
func SecurityHeaders ¶ added in v0.1.3
SecurityHeaders sets X-Content-Type-Options, X-Frame-Options, Referrer-Policy, Permissions-Policy, and Content-Security-Policy.
Types ¶
type PathFromRequest ¶
PathFromRequest returns the route pattern for the request (e.g. from chi.RouteContext). If nil, path will be "/unknown" unless status is 404 then "/not-found".