httpx

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClientIP

func ClientIP(r *http.Request, trustProxy bool) string

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

func RenderError(w http.ResponseWriter, r *http.Request, log *slog.Logger, err error)

RenderError is the one place domain errors become HTTP responses. Unknown errors become a generic 500 with detail only in the server log.

func RequestID

func RequestID(ctx context.Context) string

RequestID returns the request ID from the context, or "" if absent.

func WithHTTP

WithHTTP attaches the writer/request pair to the context.

func WithRequestID

func WithRequestID(ctx context.Context, id string) context.Context

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.

func HTTPFrom

func HTTPFrom(ctx context.Context) (HTTPPair, bool)

HTTPFrom returns the writer/request pair attached by the strict middleware.

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.

Jump to

Keyboard shortcuts

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