httpmw

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package httpmw provides framework-agnostic net/http middleware that captures request and response metadata and bodies, applying a redactor before emission.

See docs/ADR-003-httpsnoop-dependency.md for response-writer handling.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Middleware

func Middleware(cfg Config) func(http.Handler) http.Handler

Middleware returns an http.Handler middleware that captures request/response metadata and bodies, applying the redactor before emission.

Types

type Config

type Config struct {
	// Logger is the slog.Logger used to emit request/response records.
	Logger *slog.Logger

	// Redactor applies path-based redaction to captured data.
	Redactor *redact.Engine

	// CaptureRequestBody enables buffering the inbound request body.
	CaptureRequestBody bool

	// CaptureResponseBody enables buffering the outbound response body.
	CaptureResponseBody bool

	// MaxBodyBytes limits captured body size. Must be in [1024, 1048576].
	MaxBodyBytes int

	// ContentTypes restricts body capture to specific MIME types.
	ContentTypes []string

	// HeaderDenylist specifies headers to omit from logs (unless overridden by allowlist).
	HeaderDenylist []string

	// HeaderAllowlist, if non-empty, inverts the filter: only these headers are logged.
	HeaderAllowlist []string

	// SensitiveQueryParams specifies query parameter names to redact.
	SensitiveQueryParams []string

	// RequestIDHeader is the header name to check for / propagate a request ID.
	RequestIDHeader string

	// GenerateRequestID controls whether to synthesize a UUIDv4 if no ID is present.
	GenerateRequestID bool

	// SkipPaths lists request paths to skip logging (exact match).
	SkipPaths []string

	// SampleRate, when in (0, 1), logs only that fraction of requests at random.
	// A value of 0 or 1 (and any value outside (0,1)) logs every request.
	SampleRate float64

	// Clock injects a time source for deterministic testing.
	Clock func() time.Time

	// RouteFunc, if non-nil, is called after the handler returns to obtain the
	// matched route template (e.g. "/users/:id"). The return value is emitted as
	// http.route. Framework adapters (e.g. the gin adapter) use this to surface
	// the route template, which is only known after the handler chain runs.
	RouteFunc func(*http.Request) string

	// StatusFunc, if non-nil and the status captured via httpsnoop is 0, is
	// called to obtain the response status code. Needed for frameworks (e.g.
	// gin) whose ResponseWriter tracks status internally and may not call the
	// underlying http.ResponseWriter.WriteHeader through httpsnoop's wrapper.
	StatusFunc func() int
}

Config holds HTTP middleware configuration.

Jump to

Keyboard shortcuts

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