middleware

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 15, 2026 License: MIT Imports: 12 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetRequestID added in v0.1.3

func GetRequestID(ctx context.Context) string

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

func Recoverer(log logger.Logger) func(http.Handler) http.Handler

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

func RequestID() func(http.Handler) http.Handler

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

func SecurityHeaders() func(http.Handler) http.Handler

SecurityHeaders sets X-Content-Type-Options, X-Frame-Options, Referrer-Policy, Permissions-Policy, and Content-Security-Policy.

func Timeout added in v0.1.3

func Timeout(d time.Duration) func(http.Handler) http.Handler

Timeout runs the next handler with a context deadline of d. If the context is cancelled before the handler completes, responds with 503 JSON.

Types

type PathFromRequest

type PathFromRequest func(*http.Request) string

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".

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL