middleware

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const HeaderAuthorization = "Authorization"
View Source
const HeaderRequestID = "X-Request-ID"
View Source
const HeaderTenantID = "X-Tenant-ID"

Variables

This section is empty.

Functions

func Auth

func Auth(cfg AuthConfig) func(http.Handler) http.Handler

Auth authenticates requests using JWT in Authorization header and enriches context with: subject_id (sub), tenant_id, and scopes.

It never leaks token verification details to clients. All failures map to standardized errors.

func BodyLimit

func BodyLimit(maxBytes int64) func(http.Handler) http.Handler

BodyLimit limits the size of request body read by downstream handlers. It wraps r.Body with http.MaxBytesReader.

Panics if maxBytes <= 0.

func Chain

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

Chain applies middlewares to handler. Middlewares are applied in the order provided: Chain(h, A, B) => A(B(h))

func Handler

func Handler(reg *prometheus.Registry) http.Handler

Handler returns a Prometheus scrape handler. If reg is nil, it uses the global default gatherer.

func OTel

func OTel(cfg OTelConfig) func(http.Handler) http.Handler

OTel instruments inbound HTTP requests with OpenTelemetry spans. Tracer/provider configuration (resource service.name, exporters, sampling) is handled elsewhere.

func Recover

func Recover(logger *wslog.Logger) func(http.Handler) http.Handler

func RequestID

func RequestID() func(http.Handler) http.Handler

func Tenant

func Tenant(cfg TenantConfig) func(http.Handler) http.Handler

Types

type AuthConfig

type AuthConfig struct {
	Verifier *auth.Verifier

	// RequireScopes enforces that the authenticated principal has all listed scopes.
	RequireScopes []auth.Scope

	// Policy optionally performs an external policy check.
	// If Policy is set, Action and Resource must be non-empty stable strings.
	Policy   auth.PolicyChecker
	Action   string
	Resource string
}

type Metrics

type Metrics struct {
	RequestsTotal   *prometheus.CounterVec
	RequestDuration *prometheus.HistogramVec
	// contains filtered or unexported fields
}

func NewMetrics

func NewMetrics(cfg MetricsConfig) *Metrics

NewMetrics creates and registers HTTP metrics into cfg.Registry (or DefaultRegisterer if nil).

func (*Metrics) Instrument

func (m *Metrics) Instrument(route string) func(http.Handler) http.Handler

Instrument records request count and duration. route MUST be a stable identifier (no path params). Example: "POST /v1/auth/login".

type MetricsConfig

type MetricsConfig struct {
	Namespace string
	Subsystem string
	Registry  prometheus.Registerer

	// If true, status will be grouped (2xx, 4xx, 5xx) instead of exact code.
	GroupStatus bool
}

type Middleware

type Middleware func(http.Handler) http.Handler

Middleware represents an HTTP middleware function.

type OTelConfig

type OTelConfig struct {
	// Optional: customize span naming. If nil, otelhttp default is used.
	SpanNameFormatter func(operation string, r *http.Request) string
}

type TenantConfig

type TenantConfig struct {
	Mode     TenantMode
	Required bool
	Header   string

	// AllowHeaderWithoutAuth allows reading tenant_id from header even when subject_id is empty.
	// Default false. Keep this disabled for public edge handlers.
	AllowHeaderWithoutAuth bool
}

type TenantMode

type TenantMode int
const (
	// TenantFromContext uses tenant_id already present in request context (e.g. from JWT claims).
	TenantFromContext TenantMode = iota
	// TenantAllowHeader allows tenant_id override from a header (intended for internal calls only).
	TenantAllowHeader
)

Jump to

Keyboard shortcuts

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