Documentation
¶
Overview ¶
Package middleware provides the HTTP middleware shared by every route on the Schedularr API server: request-id propagation, structured request logging, panic recovery, and bearer-token authentication. internal/api/router.go wires these into the chi router built from internal/api/gen.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BearerAuth ¶
BearerAuth returns middleware that requires every request to carry "Authorization: Bearer <token>" matching token, and errors if token is shorter than 32 characters.
The presented token is never compared to the configured one directly: both are hashed with SHA-256 first and the digests are compared with crypto/subtle.ConstantTimeCompare, so a wrong guess cannot be timed to learn how many leading characters it got right. A missing or non-matching token gets a 401 application/problem+json response.
func Logging ¶
Logging returns middleware that logs one structured line per request to l after the handler completes, with method, path, status, duration_ms, and request_id keys. It should sit inside RequestID (so a request id is already in context) and outside Recovery (so a recovered panic's 500 status is reflected in the logged line).
func Recovery ¶
Recovery returns middleware that recovers from a panic anywhere in the wrapped handler chain, logs it (with a stack trace) to l, and writes a 500 problem+json response instead of letting the panic take down the server process.
func RequestID ¶
RequestID returns middleware that generates a fresh request identifier for every request, sets it on the response as X-Request-Id, and stores it in the request context so downstream handlers — and api.WriteProblem, via RequestIDFrom — can read it back.
An inbound X-Request-Id header is never trusted: a caller could use one to spoof or correlate log entries across requests it does not own, so a new id is always generated regardless of what the client sent.
func RequestIDFrom ¶
RequestIDFrom returns the request id stored in ctx by RequestID, or "" if none is present. It shares storage with problem.ContextWithRequestID / problem.RequestIDFromContext, so this is what problem.WriteProblem (and api.WriteProblem, which forwards to it) effectively reads from when it populates Problem.RequestID.
Types ¶
This section is empty.