Documentation
¶
Overview ¶
Package httpx provides plumbing-only HTTP middleware shared across services: correlation-id propagation, panic recovery, and access logging. No auth/authz logic lives here.
Index ¶
- func AccessLog(logger *slog.Logger) func(http.Handler) http.Handler
- func ActorFor(subject, rawBearer string) string
- func Correlation(next http.Handler) http.Handler
- func Ready(db Pinger) http.HandlerFunc
- func Recover(logger *slog.Logger) func(http.Handler) http.Handler
- func SubjectFromBearer(rawBearer string) string
- func WriteInternalError(w http.ResponseWriter, err error)
- type Pinger
- type StatusRecorder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AccessLog ¶
AccessLog returns middleware that logs one line per request: method, path, status, duration, and correlationId (when present).
func ActorFor ¶
ActorFor derives the audited actor for a mutation: the server-derived subject when the handler has one, else the bearer's `sub` claim (record-keeping ONLY — the authorization decision has already been made by the time an actor string is needed), else "unauthenticated".
func Correlation ¶
Correlation reads the x-correlation-id request header (accepting values up to maxCorrelationLen characters; anything longer or absent is replaced with a freshly generated id), stores it in the request context, and echoes it on the response header.
func Ready ¶
func Ready(db Pinger) http.HandlerFunc
Ready returns the GET /ready handler: 503 when the database ping fails, 200 otherwise.
func Recover ¶
Recover returns middleware that catches panics in next, logs the stack (via logger, never the client), and writes a 500 INTERNAL_ERROR envelope.
func SubjectFromBearer ¶
SubjectFromBearer returns the `sub` claim of an "Authorization: Bearer <jwt>" value WITHOUT verifying the signature, or "" when absent/malformed. It exists for audit ATTRIBUTION only: callers must already have had the bearer verified by the authz decision (or the gateway) before acting on it — this helper never grants anything, it only names the actor in audit rows so they stop reading "unauthenticated".
func WriteInternalError ¶
func WriteInternalError(w http.ResponseWriter, err error)
WriteInternalError writes the 500 envelope. err isn't included in the body (never leak internals to the client — mirrors Recover); it's swallowed here because the services have no logger of their own (the AccessLog middleware around Routes() still records the request/status for operational visibility).
Types ¶
type StatusRecorder ¶
type StatusRecorder struct {
http.ResponseWriter
Status int
}
StatusRecorder captures the status code written by the wrapped handler.
Embedding http.ResponseWriter alone satisfies the http.ResponseWriter interface, but SILENTLY DROPS every OPTIONAL interface the underlying ResponseWriter might also implement (http.Flusher for streaming/SSE, io.ReaderFrom for sendfile-style optimized copies). httputil.ReverseProxy — what the gateway wraps this recorder around for every module request — type-asserts the ResponseWriter it is given for http.Flusher directly, to stream the upstream response incrementally instead of buffering it whole (net/http/httputil/reverseproxy.go's own flush-interval logic). Flush() and ReadFrom() forward directly; Unwrap() opts into net/http's http.ResponseController "unwrap and retry" convention (net/http/responsecontroller.go), which is what makes Hijack/SetReadDeadline/SetWriteDeadline reach the real writer through this one.
func (*StatusRecorder) Flush ¶
func (s *StatusRecorder) Flush()
Flush forwards to the wrapped ResponseWriter's http.Flusher, if it implements one — a no-op otherwise.
func (*StatusRecorder) ReadFrom ¶
func (s *StatusRecorder) ReadFrom(r io.Reader) (int64, error)
ReadFrom forwards to the wrapped ResponseWriter's io.ReaderFrom, if it implements one, falling back to an ordinary io.Copy otherwise.
func (*StatusRecorder) Unwrap ¶
func (s *StatusRecorder) Unwrap() http.ResponseWriter
Unwrap returns the wrapped ResponseWriter (the http.ResponseController convention).
func (*StatusRecorder) WriteHeader ¶
func (s *StatusRecorder) WriteHeader(status int)