Documentation
¶
Overview ¶
Package httpx holds transport helpers shared across the HTTP layer: request-ID context plumbing, JSON writing, and the single problem+json error translator. It imports apperr and stdlib only.
Index ¶
- func ClientIP(r *http.Request, trustProxy bool) string
- func RenderError(w http.ResponseWriter, r *http.Request, log *slog.Logger, err error)
- func RequestID(ctx context.Context) string
- func WithHTTP(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context
- func WithRequestID(ctx context.Context, id string) context.Context
- func WriteJSON(w http.ResponseWriter, status int, v any)
- func WriteProblem(w http.ResponseWriter, r *http.Request, p Problem)
- type HTTPPair
- type Problem
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ClientIP ¶
ClientIP extracts the caller's IP. X-Forwarded-For is honored only when the deployment declares a trusted proxy (OSCTF_TRUST_PROXY); the first entry is the original client. Otherwise the socket peer address wins.
func RenderError ¶
RenderError is the one place domain errors become HTTP responses. Unknown errors become a generic 500 with detail only in the server log.
func WithRequestID ¶
WithRequestID stores the request ID in the context.
func WriteJSON ¶
func WriteJSON(w http.ResponseWriter, status int, v any)
WriteJSON serializes v as application/json with the given status.
func WriteProblem ¶
func WriteProblem(w http.ResponseWriter, r *http.Request, p Problem)
WriteProblem writes a problem+json body.
Types ¶
type HTTPPair ¶
type HTTPPair struct {
W http.ResponseWriter
R *http.Request
}
HTTPPair carries the raw request/response of the in-flight call.
type Problem ¶
type Problem struct {
Type string `json:"type"`
Title string `json:"title"`
Status int `json:"status"`
Detail string `json:"detail,omitempty"`
Errors map[string][]string `json:"errors,omitempty"`
RequestID string `json:"request_id,omitempty"`
}
Problem is the RFC 9457 body. Shape matches the OpenAPI Problem schema.