Documentation
¶
Index ¶
- Constants
- func HttpRequestLog(cfg HttpRequestLogStruct) func(http.Handler) http.Handler
- func JSONResponseMiddleware(next http.Handler) http.Handler
- func NewRouter(routes ...RouteRegistrar) http.Handler
- type AuthMiddleware
- type Authenticator
- type Field
- type HttpRequestLogStruct
- type RouteRegistrar
- type TokenValidation
Constants ¶
const ( RequestStartTrace = "REQUEST_STARTED" RequestEndTrace = "REQUEST_ENDED" )
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 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)
type Authenticator ¶ added in v0.4.0
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 ¶
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
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
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.