middleware

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2026 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Overview

Package middleware provides HTTP middleware for the PeeringDB Plus server.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CORS

func CORS(in CORSInput) func(http.Handler) http.Handler

CORS returns middleware that adds CORS headers per OPS-06. Origins are configured via the AllowedOrigins field.

func CSP added in v1.12.0

func CSP(in CSPInput) func(http.Handler) http.Handler

CSP returns middleware that sets a Content-Security-Policy-Report-Only header based on the request path. Web UI routes get a tighter policy; GraphQL gets a more permissive policy for the GraphiQL playground. Non-browser routes (/api/, /rest/, ConnectRPC) receive no CSP header.

Report-Only mode reports violations without blocking resources, allowing safe rollout and monitoring before switching to enforcing mode.

func Caching added in v1.9.0

func Caching(in CachingInput) func(http.Handler) http.Handler

Caching returns middleware that sets Cache-Control and ETag headers on GET/HEAD responses, and returns 304 Not Modified for conditional requests when data has not changed since the last sync.

Only GET and HEAD methods receive caching headers. POST and other mutation methods pass through unchanged. If no successful sync has occurred (zero sync time), no caching headers are set.

func Compression added in v1.12.0

func Compression() func(http.Handler) http.Handler

Compression returns middleware that gzip-compresses HTTP responses when the client advertises Accept-Encoding: gzip. gRPC content types are excluded because ConnectRPC manages its own compression.

Uses klauspost/compress/gzhttp which handles Content-Encoding headers, ETag suffixing (appends --gzip), and minimum size thresholds automatically.

func Logging

func Logging(logger *slog.Logger) func(http.Handler) http.Handler

Logging returns middleware that logs each HTTP request with method, path, status, duration, and trace context (trace_id, span_id) when available. Uses structured slog per OBS-1, OBS-5 with LogAttrs for attribute-based API.

func Recovery

func Recovery(logger *slog.Logger) func(http.Handler) http.Handler

Recovery returns middleware that recovers from panics in downstream handlers. Logs the panic with stack trace via slog and returns 500 to the client.

Types

type CORSInput

type CORSInput struct {
	// AllowedOrigins is a comma-separated list of allowed origins. Use "*" for all origins.
	AllowedOrigins string
}

CORSInput holds configuration for the CORS middleware.

type CSPInput added in v1.12.0

type CSPInput struct {
	// UIPolicy is the CSP directive string applied to /ui/ routes.
	UIPolicy string

	// GraphQLPolicy is the CSP directive string applied to /graphql routes.
	// Typically more permissive than UIPolicy (e.g. allows unsafe-eval for GraphiQL).
	GraphQLPolicy string
}

CSPInput holds configuration for the Content-Security-Policy middleware.

type CachingInput added in v1.9.0

type CachingInput struct {
	// SyncTimeFn returns the last successful sync completion time.
	// Returns zero time if no successful sync has occurred.
	SyncTimeFn func() time.Time

	// SyncInterval is the configured duration between automatic sync runs.
	// Used to calculate Cache-Control max-age (interval + 120s buffer).
	SyncInterval time.Duration
}

CachingInput holds configuration for the HTTP caching middleware.

Jump to

Keyboard shortcuts

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