httpx

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func AccessLog

func AccessLog(logger *slog.Logger) func(http.Handler) http.Handler

AccessLog returns middleware that logs one line per request: method, path, status, duration, and correlationId (when present).

func ActorFor

func ActorFor(subject, rawBearer string) string

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

func Correlation(next http.Handler) http.Handler

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

func Recover(logger *slog.Logger) func(http.Handler) http.Handler

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

func SubjectFromBearer(rawBearer string) string

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 Pinger

type Pinger interface {
	Ping(ctx context.Context) error
}

Pinger is the readiness dependency Ready probes — *pgxpool.Pool satisfies it.

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)

Jump to

Keyboard shortcuts

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