middleware

package
v0.0.0-...-f100c97 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	// CorrelationIDHeader is the HTTP header name for correlation ID.
	CorrelationIDHeader = gohttpkit.CorrelationIDHeader
)

Variables

This section is empty.

Functions

func CorrelationIDMiddleware

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

CorrelationIDMiddleware reads or generates X-Correlation-ID, stores it and a correlation-aware *slog.Logger in the request context, and echoes the ID in the response header.

Must be the outermost middleware so all subsequent handlers have access to a correlation-aware logger.

func ErrorHandlingMiddleware

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

ErrorHandlingMiddleware recovers from panics, logs them, and writes a 500 response using the project's ErrorResponse type.

func GetCorrelationID

func GetCorrelationID(r *http.Request) string

GetCorrelationID returns the correlation ID stored by CorrelationIDMiddleware.

func GetLogger

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

GetLogger returns the correlation-aware logger stored by CorrelationIDMiddleware, falling back to fallback when the middleware has not run.

func LoggingMiddleware

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

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

Must be registered after CorrelationIDMiddleware.

func MetricsMiddleware

func MetricsMiddleware() func(http.Handler) http.Handler

MetricsMiddleware records HTTP request metrics via Prometheus.

When registered as a per-route middleware (inside the mux, via StdHTTPServerOptions.Middlewares), r.Pattern carries the route template (e.g. "GET /api/llm-providers/{id}"), giving low-cardinality labels. When registered as outer middleware, r.Pattern is empty and r.URL.Path is used as a fallback.

Types

This section is empty.

Jump to

Keyboard shortcuts

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