middleware

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2025 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateAuthMiddleware

func CreateAuthMiddleware() []func(http.Handler) http.Handler

CreateAuthMiddleware returns a slice of middleware functions for authentication and authorization. The returned middlewares are: StoreWebToken, StoreAuthHeader, and StoreSpiffeHeader.

func CreateMiddleware

func CreateMiddleware(log *logger.Logger, auth bool) []func(http.Handler) http.Handler

CreateMiddleware creates a middleware chain with logging, tracing, and optional authentication. It attaches a request-scoped logger (using the provided logger), assigns a request ID, and propagates that ID into the logger. When auth is true, authentication middlewares (StoreWebToken, StoreAuthHeader, StoreSpiffeHeader) are included.

func GetRequestId

func GetRequestId(ctx context.Context) string

GetRequestId returns the request ID stored in ctx under keys.RequestIdCtxKey. If the value is missing or not a string, it returns the empty string.

func SentryRecoverer

func SentryRecoverer(next http.Handler) http.Handler

Recoverer implements a middleware that recover from panics, sends them to Sentry SentryRecoverer returns an http.Handler that wraps next and recovers from panics.

If a panic occurs (except http.ErrAbortHandler) the middleware logs the panic and stack trace, reports the error to the current Sentry hub, flushes Sentry events (up to 5s), and responds with HTTP 500 Internal Server Error. The returned handler otherwise delegates to next.ServeHTTP.

func SetOtelTracingContext

func SetOtelTracingContext() func(http.Handler) http.Handler

SetOtelTracingContext returns an HTTP middleware that extracts OpenTelemetry tracing context from incoming request headers and injects it into the request's context before passing the request to the next handler.

The middleware uses the global OpenTelemetry text map propagator and propagation.HeaderCarrier to read trace/span context from the request headers. Any extraction behavior (including failure handling) is delegated to the propagator implementation.

func SetRequestId

func SetRequestId() func(http.Handler) http.Handler

SetRequestId returns an HTTP middleware that ensures each request has a request ID. It reads the `X-Request-Id` header (used only if exactly one value is present); otherwise it generates a new UUID. The request ID is stored in the request context under keys.RequestIdCtxKey and the request is forwarded to the next handler with the updated context.

func SetRequestIdInLogger

func SetRequestIdInLogger() func(http.Handler) http.Handler

SetRequestIdInLogger returns HTTP middleware that injects a request-scoped logger into the request context.

The middleware loads the current logger from the request context, creates a per-request logger using logger.NewRequestLoggerFromZerolog(ctx, log.Logger), and stores the resulting logger back into the context before calling the next handler. This ensures handlers downstream receive a logger enriched for the current request.

func StoreAuthHeader

func StoreAuthHeader() func(http.Handler) http.Handler

StoreAuthHeader returns HTTP middleware that reads the request's Authorization header and stores it in the request context. The middleware wraps a handler, extracts the Authorization header (using headers.Authorization), calls appctx.AddAuthHeaderToContext with the existing request context and the header value, and invokes the next handler with the request updated to use that context. If the Authorization header is absent or empty, nothing is stored.

func StoreLoggerMiddleware

func StoreLoggerMiddleware(log *logger.Logger) func(http.Handler) http.Handler

StoreLoggerMiddleware returns an HTTP middleware that injects the provided logger into each request's context so downstream handlers can retrieve it.

func StoreSpiffeHeader

func StoreSpiffeHeader() func(http.Handler) http.Handler

StoreSpiffeHeader returns an HTTP middleware that extracts a SPIFFE URL from the request headers and, if present, inserts it into the request context for downstream handlers.

The middleware always calls the next handler; when a SPIFFE URL is found it updates the request's context with that value so subsequent handlers can retrieve it.

func StoreWebToken

func StoreWebToken() func(http.Handler) http.Handler

StoreWebToken returns middleware that extracts a JWT from the HTTP `Authorization` header and stores it in the request pmcontext for downstream handlers.

The middleware looks for an Authorization header of the form `Bearer <token>` (scheme match is case-insensitive). When present, the token is added to the pmcontext via context.AddWebTokenToContext using the package's signatureAlgorithms. If the header is absent, malformed, or not a Bearer token, the request pmcontext is left unchanged.

Types

This section is empty.

Jump to

Keyboard shortcuts

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