middleware

package
v0.8.4 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RequestStartTrace = "REQUEST_STARTED"
	RequestEndTrace   = "REQUEST_ENDED"
)
View Source
const UserIDKey contextKey = "userID"

Variables

This section is empty.

Functions

func HttpRequestLog

func HttpRequestLog(cfg HttpRequestLogStruct) func(http.Handler) http.Handler

HttpRequestLog returns stdlib-compatible middleware (func(http.Handler) http.Handler) that logs a start and end event per request, and attaches a request-scoped logger to the request context (retrievable via logger.FromContext). Each registration can pass its own cfg, e.g. r.Use(HttpRequestLog(cfg)).

func JSONResponseMiddleware added in v0.4.0

func JSONResponseMiddleware(next http.Handler) http.Handler

func NewRouter added in v0.4.0

func NewRouter(routes ...RouteRegistrar) http.Handler

NewRouter builds the base router with standard middleware, then hands control to each registrar to register its own routes.

Types

type AuthMiddleware added in v0.4.0

type AuthMiddleware struct {
	// contains filtered or unexported fields
}

func NewAuthMiddleware added in v0.4.0

func NewAuthMiddleware(secret string, v TokenValidation) (*AuthMiddleware, error)

func (*AuthMiddleware) Verify added in v0.4.0

func (m *AuthMiddleware) Verify(next http.Handler) http.Handler

type Authenticator added in v0.4.0

type Authenticator interface {
	Verify(next http.Handler) http.Handler
}

Authenticator verifies an incoming request and attaches identity to context. AuthMiddleware is the built-in JWT implementation; consumers needing a different auth scheme can implement their own.

type Field

type Field struct {
	Key   string
	Value func(r *http.Request) any
}

Field is an extra request-scoped log field, e.g. userID, app version, route pattern. Consumers own how it's derived (chi route pattern, JWT claims, custom headers, etc.).

type HttpRequestLogStruct

type HttpRequestLogStruct struct {
	// RequestID extracts the request ID (default: X-Request-Id header).
	RequestID func(r *http.Request) string
	// Fields are extra fields logged on both the start and end events, in order.
	Fields []Field
	// LogBody captures the response body on the end-of-request log.
	LogBody bool
}

HttpRequestLogStruct configures HttpRequestLog.

type RouteRegistrar added in v0.4.0

type RouteRegistrar func(r chi.Router)

RouteRegistrar registers routes on r. It closes over whatever Authenticator(s) it needs (zero, one, or several) - NewRouter has no opinion on auth.

type TokenValidation added in v0.7.4

type TokenValidation func(ctx context.Context, token *jwt.Token) (context.Context, error)

TokenValidation checks a parsed token against external state (e.g. revocation or DB-tracked expiry) and returns ctx enriched with claims-derived values. Returning an error 401s the request. Optional - nil skips this check and leaves ctx untouched.

Jump to

Keyboard shortcuts

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