middleware

package
v0.0.0-...-25b0c30 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package middleware provides HTTP middleware utilities for the httprpc framework.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CORS

func CORS(cfg CORSConfig) httprpc.Middleware

CORS returns middleware applying the provided CORSConfig.

func Logging

func Logging(logger *slog.Logger) httprpc.Middleware

Logging logs request/response metadata using slog. If logger is nil, slog.Default is used.

func Recover

func Recover(logger *slog.Logger) httprpc.Middleware

Recover returns middleware that recovers from panics and writes a 500 response. If logger is nil, slog.Default is used.

func RequestID

func RequestID(headerName string) httprpc.Middleware

RequestID injects a request ID into the context and response headers. If headerName is empty, "X-Request-ID" is used. If the incoming request already has the header, it is propagated.

func RequestIDFromContext

func RequestIDFromContext(ctx context.Context) (string, bool)

RequestIDFromContext returns the request ID set by RequestID middleware, if present.

func RequestSizeLimit

func RequestSizeLimit(maxBytes int64) httprpc.Middleware

RequestSizeLimit limits the readable request body to maxBytes using http.MaxBytesReader.

func ReverseProxy

func ReverseProxy(cfg ReverseProxyConfig) httprpc.Middleware

ReverseProxy returns a middleware that proxies matching requests to the target. If MatchPrefix is set and the path doesn't match, the request is passed to the next handler.

func ReverseProxyHandler

func ReverseProxyHandler(cfg ReverseProxyConfig) http.Handler

ReverseProxyHandler returns an http.Handler that proxies requests to the target.

func Timeout

func Timeout(d time.Duration) httprpc.Middleware

Timeout sets a context timeout for each request. If d is zero or negative, it is a no-op.

Types

type CORSConfig

type CORSConfig struct {
	AllowedOrigins   []string
	AllowedMethods   []string
	AllowedHeaders   []string
	ExposeHeaders    []string
	AllowCredentials bool
	MaxAgeSeconds    int
}

CORSConfig configures CORS behavior. For production, adjust the allowed origins and methods to your needs.

type ReverseProxyConfig

type ReverseProxyConfig struct {
	Target *url.URL

	// MatchPrefix, if set, limits proxying to paths with this prefix.
	// When used with the middleware form, requests that don't match fall through to next.
	MatchPrefix string

	// StripPrefix removes the given prefix from the request path before forwarding.
	StripPrefix string

	// PreserveHost keeps the incoming Host header instead of rewriting to the target's host.
	PreserveHost bool

	// ErrorHandler handles proxy errors. If nil, a 502 Bad Gateway is returned.
	ErrorHandler func(http.ResponseWriter, *http.Request, error)
}

ReverseProxyConfig configures the reverse proxy middleware/handler. Target is required.

Jump to

Keyboard shortcuts

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