middleware

package
v0.0.0-...-ba2e97a Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package middleware provides composable net/http middleware following the standard func(http.Handler) http.Handler pattern.

Index

Constants

View Source
const CorrelationIDHeader = "X-Correlation-ID"

CorrelationIDHeader is the canonical header name for the correlation / trace ID.

Variables

View Source
var DefaultAllowedHeaders = []string{"Content-Type", "Authorization", "X-Correlation-ID"}

DefaultAllowedHeaders are the headers permitted by default when building CORSOptions.

Functions

func CORSMiddleware

func CORSMiddleware(opts CORSOptions) func(http.Handler) http.Handler

CORSMiddleware applies CORS headers globally to every route handled by the wrapped handler.

func Chain

func Chain(mw ...func(http.Handler) http.Handler) func(http.Handler) http.Handler

Chain composes middleware left-to-right so that the first argument is the outermost wrapper (executed first on a request, last on a response).

Chain(A, B, C)(handler) == A(B(C(handler)))

func CorrelationIDMiddleware

func CorrelationIDMiddleware(log *slog.Logger) func(http.Handler) http.Handler

CorrelationIDMiddleware reads the X-Correlation-ID request header (or generates a new UUID v4 if absent), stores it and a correlation-aware *slog.Logger in the request context, and echoes the ID in the response header.

Register this as the outermost middleware so all subsequent handlers have access to a correlation-aware logger.

func GetCorrelationID

func GetCorrelationID(r *http.Request) string

GetCorrelationID returns the correlation ID stored by CorrelationIDMiddleware. Returns an empty string when the middleware has not run.

func GetLogger

func GetLogger(r *http.Request, fallback *slog.Logger) *slog.Logger

GetLogger returns the correlation-aware logger stored by CorrelationIDMiddleware. Falls back to fallback when the middleware has not run (e.g. in tests).

func LoggingMiddleware

func LoggingMiddleware(fallback *slog.Logger) func(http.Handler) http.Handler

LoggingMiddleware logs each request (method, path, status, latency) via slog after the downstream handler completes. It uses the correlation-aware logger stored by CorrelationIDMiddleware when available.

Must be registered after CorrelationIDMiddleware to capture correlation IDs.

func RecoveryMiddleware

func RecoveryMiddleware(fallback *slog.Logger) func(http.Handler) http.Handler

RecoveryMiddleware catches panics from downstream handlers, logs the stack trace, and writes a 500 JSON response so the server stays alive.

func WithCORS

func WithCORS(pattern string, h http.HandlerFunc, opts CORSOptions) (string, http.HandlerFunc)

WithCORS wraps handler with per-route CORS and returns the (pattern, wrappedHandler) pair so it can be spread directly into mux.HandleFunc:

mux.HandleFunc(middleware.WithCORS("GET /users/{id}", handler, opts))

Types

type CORSOptions

type CORSOptions struct {
	// AllowedOrigins lists origins that may access the resource.
	// Empty (the default) denies all cross-origin access — fail closed.
	// Use ["*"] to allow any origin (cannot be combined with AllowCredentials).
	AllowedOrigins []string
	// AllowedMethods lists the HTTP methods permitted for the resource.
	AllowedMethods []string
	// AllowedHeaders lists the request headers the browser may send.
	AllowedHeaders []string
	// AllowCredentials indicates whether cookies / auth headers are allowed.
	AllowCredentials bool
	// MaxAge is the number of seconds the preflight response may be cached.
	MaxAge int
}

CORSOptions configures CORS behaviour for CORSMiddleware and WithCORS.

Jump to

Keyboard shortcuts

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