middleware

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package middleware supplies composable native HTTP middleware.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Chain

func Chain(h http.Handler, m ...Middleware) http.Handler

Chain applies middleware in left-to-right request order.

func ClientIP

func ClientIP(r *http.Request, trusted []*net.IPNet) net.IP

ClientIP trusts X-Forwarded-For only for explicitly trusted proxy CIDRs, peeling from the right.

func Compression

func Compression(next http.Handler) http.Handler

Compression enables gzip when accepted, excluding protocol upgrades and bodyless responses.

func RequestID

func RequestID(next http.Handler) http.Handler

RequestID generates a server-owned request ID and adds it to logging context.

func SecurityHeaders

func SecurityHeaders(next http.Handler) http.Handler

SecurityHeaders sets conservative browser response security headers.

Types

type CORSConfig

type CORSConfig struct {
	Origins, Methods, Headers []string
	Credentials               bool
	MaxAge                    time.Duration
}

CORSConfig contains explicitly allowed origins, methods, headers and credential policy.

type Middleware

type Middleware func(http.Handler) http.Handler

Middleware returns context-aware HTTP admission or coordination middleware.

func Authenticate

func Authenticate(check func(*http.Request) (context.Context, error)) Middleware

Authenticate attaches a verified identity context before authorization. Failure responses never expose verifier diagnostics.

func Authorize

func Authorize(check func(*http.Request) error) Middleware

Authorize applies an explicit caller-supplied authorization policy.

func BodyLimit

func BodyLimit(n int64) Middleware

BodyLimit limits request-body reading and rejects oversized declared bodies.

func CORS

func CORS(c CORSConfig) (Middleware, error)

CORS validates CORS policy and returns its native middleware.

func ContentType

func ContentType(want string) Middleware

ContentType enforces a parsed request MIME type when a body is present.

func CorrelationID

func CorrelationID(trustForwarded bool) Middleware

CorrelationID generates a server-owned correlation ID unless a bounded forwarded ID is explicitly trusted.

func Logging

func Logging(l *slog.Logger) Middleware

Logging returns structured request-logging instrumentation.

func Recovery

func Recovery(report func(context.Context, error)) Middleware

Recovery returns a reporting panic-recovery interceptor or middleware.

func Timeout

func Timeout(d time.Duration) Middleware

Timeout uses the standard buffered HTTP timeout handler; it is unsuitable for streaming.

Jump to

Keyboard shortcuts

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