middleware

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 16 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package middleware contains HTTP middlewares that wrap a HTTP handler to provide ancillary functionality such as capturing HTTP details into the request context or printing debug information on incoming requests.

Index

Constants

View Source
const (
	// RequestMethodKey is the request context key used to store r.Method created by
	// the PopulateRequestContext middleware.
	RequestMethodKey ctxKey = iota + 1

	// RequestURIKey is the request context key used to store r.RequestURI created by
	// the PopulateRequestContext middleware.
	RequestURIKey

	// RequestPathKey is the request context key used to store r.URL.Path created by
	// the PopulateRequestContext middleware.
	RequestPathKey

	// RequestProtoKey is the request context key used to store r.Proto created by
	// the PopulateRequestContext middleware.
	RequestProtoKey

	// RequestHostKey is the request context key used to store r.Host created by
	// the PopulateRequestContext middleware.
	RequestHostKey

	// RequestRemoteAddrKey is the request context key used to store r.RemoteAddr
	// created by the PopulateRequestContext middleware.
	RequestRemoteAddrKey

	// RequestXForwardedForKey is the request context key used to store the
	// X-Forwarded-For header created by the PopulateRequestContext middleware.
	RequestXForwardedForKey

	// RequestXForwardedProtoKey is the request context key used to store the
	// X-Forwarded-Proto header created by the PopulateRequestContext middleware.
	RequestXForwardedProtoKey

	// RequestXRealIPKey is the request context key used to store the
	// X-Real-IP header created by the PopulateRequestContext middleware.
	RequestXRealIPKey

	// RequestAuthorizationKey is the request context key used to store the
	// Authorization header created by the PopulateRequestContext middleware.
	RequestAuthorizationKey

	// RequestRefererKey is the request context key used to store Referer header
	// created by the PopulateRequestContext middleware.
	RequestRefererKey

	// RequestUserAgentKey is the request context key used to store the User-Agent
	// header created by the PopulateRequestContext middleware.
	RequestUserAgentKey

	// RequestXRequestIDKey is the request context key used to store the X-Request-Id
	// header created by the PopulateRequestContext middleware.
	RequestXRequestIDKey

	// RequestAcceptKey is the request context key used to store the Accept header
	// created by the PopulateRequestContext middleware.
	RequestAcceptKey

	// RequestXCSRFTokenKey is the request context key used to store X-Csrf-Token header
	// created by the PopulateRequestContext middleware.
	RequestXCSRFTokenKey
)

Variables

This section is empty.

Functions

func Debug

func Debug(mux loomhttp.Muxer, w io.Writer, fields ...diagnostics.Fields) func(http.Handler) http.Handler

Debug returns a debug middleware which prints detailed information about incoming requests and outgoing responses including all headers, parameters and bodies. Each body preview is limited to 64 KiB; oversized previews are omitted with an explicit marker, without limiting application bytes. Request capture observes downstream reads without draining or replacing errors. A preview without observed EOF is marked incomplete. The summary is emitted when the handler returns. Response writer capabilities are preserved. Conventional credentials and supplied wire credential fields are masked using an immutable copy of fields; application requests and responses stay unchanged.

func PopulateRequestContext

func PopulateRequestContext() func(http.Handler) http.Handler

PopulateRequestContext returns a middleware which populates a number of standard HTTP header values into the request context. Those values may be extracted using the corresponding ContextKey type in this package.

func ProblemRateLimitResponse added in v1.12.0

func ProblemRateLimitResponse(header http.Header, r *http.Request, rejection RateLimitRejection) []byte

ProblemRateLimitResponse is a RateLimitResponder that renders the rejection as the RFC 9457 application/problem+json document Loom uses for service errors, with code "rate_limited" or "rate_limit_unavailable" and the rejection status.

func RateLimit added in v1.10.0

func RateLimit(control *admission.RateAdmission, key func(*http.Request) (string, error), opts ...RateLimitOption) func(http.Handler) http.Handler

RateLimit admits HTTP requests before invoking the next handler. Applications choose a trusted grouping function and local or shared quota backend. Quota denial returns 429 and a whole-second Retry-After when known. Grouping/backend errors and timeouts return 503 with no sensitive details. No retry occurs. Rejections use a text/plain status text body unless WithRateLimitResponder selects another representation; the status and Retry-After are unchanged.

Place early limits outside the generated handler to protect decoding and authentication. Authenticated quotas belong at protected endpoint middleware. The request/context is passed unchanged after admission. Do not derive keys from forwarded headers without a trusted proxy policy, or expose keys as metric labels. Construction panics for nil dependencies, including a nil responder.

func RequestContext

func RequestContext(ctx context.Context) func(http.Handler) http.Handler

RequestContext returns a middleware which initializes the request context.

func RequestContextKeyVals

func RequestContextKeyVals(keyvals ...any) func(http.Handler) http.Handler

RequestContextKeyVals returns a middleware which adds the given key/value pairs to the request context.

func SmartRedirectSlashes

func SmartRedirectSlashes(next http.Handler) http.Handler

SmartRedirectSlashes is a middleware that matches the request path with patterns added to the router and redirects it.

If a pattern is added to the router with a trailing slash, any matches on that pattern without a trailing slash will be redirected to the version with the slash. If a pattern does not have a trailing slash, matches on that pattern with a trailing slash will be redirected to the version without.

This middleware depends on chi, so it needs to be mounted on chi's router. It make the router behavior similar to httptreemux.

Types

type RateLimitOption added in v1.12.0

type RateLimitOption func(*rateLimitConfig)

RateLimitOption configures RateLimit.

func WithRateLimitResponder added in v1.12.0

func WithRateLimitResponder(respond RateLimitResponder) RateLimitOption

WithRateLimitResponder selects how RateLimit renders rejection bodies.

type RateLimitRejection added in v1.12.0

type RateLimitRejection struct {
	// Status is http.StatusTooManyRequests for quota denial or
	// http.StatusServiceUnavailable for grouping and backend failures.
	Status int
	// Decision is the denial decision for a 429 rejection. It is zero for a
	// 503 rejection.
	Decision admission.RateDecision
	// Err is a safe temporary *loom.ServiceError: "rate_limited" for quota
	// denial or "rate_limit_unavailable" for failures. It never contains
	// grouping keys or backend error text.
	Err error
}

RateLimitRejection describes one request that RateLimit refuses.

type RateLimitResponder added in v1.12.0

type RateLimitResponder func(header http.Header, r *http.Request, rejection RateLimitRejection) []byte

RateLimitResponder renders the body of a rejection. It may set response headers, such as Content-Type, on header and returns the body bytes. RateLimit owns the status code and framing: it writes rejection.Status, replaces any Retry-After set by the responder and removes any Content-Length. A nil body writes no body.

Directories

Path Synopsis
Package otel provides thin OpenTelemetry middleware helpers for Loom HTTP servers and clients.
Package otel provides thin OpenTelemetry middleware helpers for Loom HTTP servers and clients.

Jump to

Keyboard shortcuts

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