Documentation
¶
Overview ¶
Package reqctx carries request-scoped metadata — a request/correlation ID, the client IP, the User-Agent, and the authenticated user's ID — through a context.Context, independent of any HTTP router, logging framework or identity provider. It depends on nothing beyond the standard library.
reqctx exists as neutral ground between two layers that must never import each other:
- httpx/middleware (the transport layer) legitimately knows about the inbound *http.Request and chi's request-ID middleware, and is the writer: it populates a request ID, IP, and User-Agent here for every inbound request.
- logger and audit (application-facing layers) read this metadata without depending on httpx or chi. logger stamps every log line with the request ID; audit stamps every audit_log row with it. Because both read the exact same context key owned by this package, the request ID that reaches a log line and the one that reaches an audit row are provably the same value — see audit.Recorder.Record.
Index ¶
- func ClientRequestIDFromContext(ctx context.Context) string
- func RequestIDFromContext(ctx context.Context) string
- func SetUserID(ctx context.Context, id string)
- func UserIDFromContext(ctx context.Context) string
- func WithClientRequestID(ctx context.Context, id string) context.Context
- func WithRequestID(ctx context.Context, id string) context.Context
- func WithRequestInfo(ctx context.Context, info RequestInfo) context.Context
- func WithUserSlot(ctx context.Context) context.Context
- type RequestInfo
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ClientRequestIDFromContext ¶ added in v0.9.0
ClientRequestIDFromContext returns the client-supplied correlation ID previously stored via WithClientRequestID, or "" if none is present.
func RequestIDFromContext ¶
RequestIDFromContext returns the request/correlation ID previously stored via WithRequestID, or "" if none is present.
func SetUserID ¶ added in v0.9.0
SetUserID records id as the authenticated user's ID in the slot ctx carries. It is a no-op when ctx carries no slot (WithUserSlot was never called upstream), so an authentication middleware mounted without httpx/middleware.RequestContext keeps working and the ID is simply not logged.
Only an opaque, stable user identifier belongs here — never a username, roles, or a credential: the value ends up in every access-log line.
func UserIDFromContext ¶ added in v0.9.0
UserIDFromContext returns the user ID previously recorded via SetUserID, or "" if ctx carries no slot or the slot was never filled (an unauthenticated request).
func WithClientRequestID ¶ added in v0.9.0
WithClientRequestID returns a copy of ctx carrying id as the correlation ID the client sent (e.g. its X-Request-Id header). It is untrusted input: httpx/middleware.RequestID sanitizes and length-limits it before storing it here, and it never replaces the server-generated request ID returned by RequestIDFromContext.
func WithRequestID ¶
WithRequestID returns a copy of ctx carrying id as the request/correlation ID. Callers that bridge a specific transport's request ID (e.g. an HTTP middleware reading chi's request ID) call this to make the ID available to logger.Logger.WithContext and audit.Recorder.Record without either of those packages depending on that transport.
func WithRequestInfo ¶
func WithRequestInfo(ctx context.Context, info RequestInfo) context.Context
WithRequestInfo returns a copy of ctx carrying info as the request's client IP and User-Agent.
This is the only way to inject RequestInfo into a context: no context key is exported. Production code populates it via httpx/middleware.RequestContext; tests in any package call this directly instead of reaching into a leaky exported key.
func WithUserSlot ¶ added in v0.9.0
WithUserSlot returns a copy of ctx carrying an empty user slot that SetUserID can fill later through any context derived from it. If ctx already carries a slot, ctx is returned unchanged: a second, inner slot would shadow the first, and a middleware holding the outer context would never see a user ID written through the inner one.
Production code installs the slot via httpx/middleware.RequestContext.
Types ¶
type RequestInfo ¶
RequestInfo holds the client IP and User-Agent extracted from an inbound HTTP request.
func RequestInfoFromContext ¶
func RequestInfoFromContext(ctx context.Context) (RequestInfo, bool)
RequestInfoFromContext returns the RequestInfo previously stored via WithRequestInfo, and whether one was present.