nethttp

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package nethttp adapts api/rest route handles to net/http handlers.

Each [RouteHandle] from api/rest becomes an http.Handler via Handler. Register wires it directly onto an http.ServeMux using the Go 1.22+ method-prefixed pattern ("POST /users", "GET /users/{id}", etc.).

Typical usage:

b := rest.NewBuilder(rest.Info{Title: "User API", Version: "1.0.0"})
createUser, _ := rest.NewRoute[CreateReq, User]("POST", "/users", ...).Register(b)

mux := http.NewServeMux()
nethttp.Register(mux, createUser, func(ctx context.Context, req CreateReq) (User, error) {
    // Access path params via the embedded request:
    r, _ := nethttp.RequestFromContext(ctx)
    id := r.PathValue("id")
    return svc.CreateUser(ctx, req)
}, nethttp.Options{})
http.ListenAndServe(":8080", mux)

Error responses use the JSON body {"error":"<message>"} by default: 400 for decode/validation failures, 500 for handler or encode errors. Override via Options.ErrorHandler.

For body-less methods (GET, HEAD, DELETE) the handler function is called with the zero value of Req. Access path and query parameters through RequestFromContext.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Call added in v0.10.0

func Call[Req, Resp any](
	ctx context.Context,
	client *http.Client,
	baseURL string,
	handle *rest.RouteHandle[Req, Resp],
	req Req,
	vars map[string]string,
	opts CallOptions,
) (Resp, error)

Call executes a typed HTTP request for the given route handle against baseURL.

The concrete URL is built as baseURL + handle.BuildPath(vars) + "?" + queryString. For body-bearing methods (POST, PUT, PATCH) req is JSON-encoded as the request body; for other methods (GET, HEAD, DELETE) req is ignored.

All parameters are validated against their registered codecs before the request is sent: path variables via rest.RouteHandle.BuildPath, query parameters via rest.RouteHandle.ValidateQuery, cookies via rest.RouteHandle.ValidateCookies, and request headers via rest.RouteHandle.ValidateHeaders. A validation failure returns the corresponding rest error type (e.g. rest.PathParamError, rest.QueryParamError) without sending any request.

Security requirements: if the route declares non-nil Security (or inherits global security), CallOptions.CredentialFunc is called to obtain the Authorization headers. A nil CredentialFunc on a secured route is not an error — the request is sent without credential injection; use CallOptions.ExtraHeaders to supply static credentials instead.

On a 2xx response the body is decoded into Resp using the route's response codec. On a non-2xx response UnexpectedStatusError is returned.

CallOptions.Observer receives stats.Observer.RecordRequest after every call (success or failure) with the route path template (not the concrete URL), status code, and total duration. Per-field validation errors are reported separately via stats.Observer.RecordValidationError.

Example — GET with path variable:

handle := getUserRoute.ClientHandle()
user, err := nethttp.Call(ctx, http.DefaultClient, "https://api.example.com",
    handle, struct{}{}, map[string]string{"id": "f47ac10b"},
    nethttp.CallOptions{Observer: obs})

Example — POST with body and bearer token:

handle := createUserRoute.ClientHandle()
resp, err := nethttp.Call(ctx, http.DefaultClient, "https://api.example.com",
    handle, createReq, nil,
    nethttp.CallOptions{
        CredentialFunc: func(ctx context.Context, reqs []route.SecurityRequirement) (http.Header, error) {
            h := make(http.Header)
            h.Set("Authorization", "Bearer "+token)
            return h, nil
        },
    })
Example
// Define the route — use ClientHandle() when no OpenAPI spec is needed.
type Item struct{ ID, Name string }
itemCodec := codex.Struct[Item](
	codex.OptionalField("id", codex.String(),
		func(i Item) string { return i.ID },
		func(i *Item, v string) { i.ID = v },
	),
	codex.RequiredField("name", codex.String().Refine(validate.NonEmptyString),
		func(i Item) string { return i.Name },
		func(i *Item, v string) { i.Name = v },
	),
)
getRoute := rest.NewRoute[getReq, Item]("GET", "/items/{id}",
	codex.Struct[getReq](), itemCodec,
	rest.PathParam{Name: "id"}.WithCodec(codex.String().Refine(validate.NonEmptyString)),
).ClientHandle()

// Validate path params before any HTTP call.
_, err := nethttp.Call(context.Background(), http.DefaultClient, "https://api.example.com",
	getRoute, getReq{}, map[string]string{"id": ""},
	nethttp.CallOptions{})
if err != nil {
	var pathErr rest.PathParamError
	if errors.As(err, &pathErr) {
		fmt.Printf("param %q rejected: %v\n", pathErr.Name, pathErr.Err)
	}
}
Output:
param "id" rejected: constraint failed (non-empty): expected non-empty string

func Handler

func Handler[Req, Resp any](handle *rest.RouteHandle[Req, Resp], fn HandlerFunc[Req, Resp], opts Options) http.Handler

Handler wraps a rest.RouteHandle and a HandlerFunc into an http.Handler.

For body-bearing methods (POST, PUT, PATCH) the request body is read, decoded, and validated using the route's codec before fn is called. For other methods (GET, HEAD, DELETE) fn is called with the zero value of Req.

On success the response is JSON-encoded and written with the HTTP status from the route descriptor's primary response (the first entry in Responses).

Pass a zero-value Options{} for default behaviour (JSON error envelope, 1 MiB body limit, application/json Content-Type check, no-op observer).

func Register

func Register[Req, Resp any](mux *http.ServeMux, handle *rest.RouteHandle[Req, Resp], fn HandlerFunc[Req, Resp], opts Options)

Register registers the route on mux using its method and path from the route descriptor. It uses the Go 1.22+ enhanced ServeMux pattern "METHOD /path" so each registration is scoped to a single method.

Pass a zero-value Options{} for default behaviour.

func RegisterSSE added in v0.8.0

func RegisterSSE[Req, Event any](mux *http.ServeMux, handle *rest.SSERouteHandle[Req, Event], fn SSEHandlerFunc[Req, Event], opts Options)

RegisterSSE wires an rest.SSERouteHandle onto mux as a GET SSE endpoint.

func RequestFromContext

func RequestFromContext(ctx context.Context) (*http.Request, bool)

RequestFromContext retrieves the *http.Request stored in ctx by Handler. Returns false if the context was not created by this package.

func ResponseHeadersFromContext added in v0.8.0

func ResponseHeadersFromContext(ctx context.Context) (http.Header, bool)

ResponseHeadersFromContext retrieves response headers previously stored by WithResponseHeaders. Returns false if no headers were set.

func SSEHandler added in v0.8.0

func SSEHandler[Req, Event any](handle *rest.SSERouteHandle[Req, Event], fn SSEHandlerFunc[Req, Event], opts Options) http.Handler

SSEHandler wraps an rest.SSERouteHandle and a user-supplied SSEHandlerFunc into an http.Handler that streams Server-Sent Events.

The handler sets Content-Type: text/event-stream, Cache-Control: no-cache, and Connection: keep-alive, then calls fn. The send func provided to fn validates the event via the codec, encodes it as JSON, writes "data: <json>\n\n" to the response, and flushes. If the event fails validation, send returns an error without writing anything.

fn should honour ctx.Done() for clean client-disconnect handling.

func SetCookie added in v0.8.0

func SetCookie(w http.ResponseWriter, name, value string, opts CookieOptions) error

SetCookie writes a Set-Cookie header on w with secure defaults: Secure, HttpOnly, SameSite=Strict, Path="/".

If CookieOptions.Codec is non-nil, value is validated first using that codec. A validation failure returns a rest.CookieParamError without writing any header — the same error type returned by rest.RouteHandle.ValidateCookies on the read side.

Example — symmetric read/write validation from a single codec:

sessionCodec := codex.String().Refine(validate.MinLen(32))

// Read (adapter validates automatically via CookieParam):
rest.CookieParam{Name: "session_token"}.WithCodec(sessionCodec)

// Write (handler sets the cookie with the same codec):
if err := nethttp.SetCookie(w, "session_token", token,
    nethttp.CookieOptions{MaxAge: 3600}.WithCodec(sessionCodec),
); err != nil {
    http.Error(w, err.Error(), http.StatusInternalServerError)
    return
}

func WithResponseCookies added in v0.8.0

func WithResponseCookies(ctx context.Context, cookies ...PendingCookie)

WithResponseCookies deposits one or more PendingCookie values into ctx. Handler validates their values against the route's [ResponseCookieParam] codecs and then writes Set-Cookie headers on success. Call this inside a HandlerFunc to emit response cookies.

func WithResponseHeaders added in v0.8.0

func WithResponseHeaders(ctx context.Context, h http.Header)

WithResponseHeaders copies the key-value pairs from h into the response header map stored in ctx (pre-allocated by Handler before calling the HandlerFunc). Call this inside a HandlerFunc to emit response headers such as Location, ETag, or custom headers without direct access to http.ResponseWriter.

resp, _ := svc.Create(ctx, req)
h := make(http.Header)
h.Set("Location", "/users/"+resp.ID)
nethttp.WithResponseHeaders(ctx, h) // mutates the header map in ctx

Types

type CallOptions added in v0.10.0

type CallOptions struct {
	// QueryParams appends query string parameters to the URL.
	// Each value is validated against its registered [rest.QueryParam] codec (if any)
	// before the request is sent.
	QueryParams map[string]string

	// CookieParams adds cookies to the outgoing request.
	// Each value is validated against its registered [rest.CookieParam] codec (if any)
	// before the request is sent.
	CookieParams map[string]string

	// HeaderParams adds declared request headers to the outgoing request.
	// Each value is validated against its registered [rest.HeaderParam] codec (if any)
	// before the request is sent.
	//
	// Do not pass the Authorization header here — use [CallOptions.ExtraHeaders] or
	// [CallOptions.CredentialFunc] for security credentials.
	HeaderParams map[string]string

	// ExtraHeaders adds arbitrary HTTP headers to the outgoing request without
	// codec validation. Use for non-declared headers such as X-Request-ID,
	// User-Agent, or static Authorization values.
	ExtraHeaders http.Header

	// CredentialFunc, when non-nil, is called for routes that declare non-nil
	// Security requirements. It receives the effective security requirements and
	// must return headers to merge into the outgoing request (e.g. Authorization).
	// Return a non-nil error to abort the call before the request is sent.
	//
	// Use [CallOptions.ExtraHeaders] for simple static credentials;
	// use CredentialFunc for structured or dynamic credential injection —
	// it mirrors the server-side SecurityFunc pattern.
	CredentialFunc func(ctx context.Context, reqs []route.SecurityRequirement) (http.Header, error)

	// Observer, when non-nil, receives per-call lifecycle events.
	// [stats.Observer.RecordRequest] is called on every code path — including
	// early-exit validation failures — with the HTTP method, route path template
	// (not the concrete URL), HTTP status code, and total duration.
	// Status 0 is used when validation fails before any HTTP request is sent
	// (path var, query, cookie, or header codec failure; credential func error;
	// or request build error). This allows observers to count all call attempts,
	// including those that never reach the network.
	// Per-field validation errors are reported via [stats.Observer.RecordValidationError].
	// Defaults to [stats.NoopObserver] when nil.
	Observer stats.Observer
}

CallOptions configures an outgoing HTTP request made via Call.

type CookieOptions added in v0.8.0

type CookieOptions struct {
	// Path is the cookie path. Defaults to "/" when empty.
	Path string

	// Domain scopes the cookie to a specific host. Defaults to the current host.
	Domain string

	// MaxAge is the cookie lifetime in seconds.
	// 0 means session cookie (deleted when browser closes).
	// Negative means delete the cookie immediately.
	MaxAge int

	// SameSite controls the cross-site request behaviour.
	// Defaults to [http.SameSiteStrictMode] when zero.
	SameSite http.SameSite

	// Insecure, when true, omits the Secure attribute.
	// Use only for non-TLS environments such as localhost development.
	// Default: false (Secure is always set).
	Insecure bool

	// AllowJS, when true, omits the HttpOnly attribute, making the cookie
	// accessible via document.cookie. Required for patterns such as
	// CSRF tokens that must be read by client-side JavaScript.
	// Default: false (HttpOnly is always set).
	AllowJS bool

	// Codec, when non-nil, validates value before the Set-Cookie header is
	// written. Use the same [codex.Codec] as the matching [rest.CookieParam]
	// for symmetric read/write validation from a single definition.
	// Set via [CookieOptions.WithCodec] to avoid address-of boilerplate.
	//
	// If validation fails, SetCookie returns [rest.CookieParamError] and does
	// NOT write the Set-Cookie header.
	Codec *codex.Codec[string]
}

CookieOptions configures the security attributes and optional value validation applied by SetCookie.

Safe defaults: Secure=true, HttpOnly=true, SameSite=Strict, Path="/". Use the opt-in fields to relax specific attributes for legitimate use cases.

func (CookieOptions) WithCodec added in v0.8.0

func (o CookieOptions) WithCodec(c codex.Codec[string]) CookieOptions

WithCodec sets the validation codec and returns the updated CookieOptions. Avoids the temporary-variable + address-of pattern required when setting Codec inline:

err := nethttp.SetCookie(w, "session_token", token,
    nethttp.CookieOptions{MaxAge: 3600}.WithCodec(sessionCodec))

type HandlerFunc

type HandlerFunc[Req, Resp any] func(ctx context.Context, req Req) (Resp, error)

HandlerFunc is the typed application handler called by Handler. ctx is the request context. req is the decoded request value; for body-less methods it is the zero value of Req. Use RequestFromContext to access the underlying *http.Request for path parameters, headers, or other request metadata.

type Options

type Options struct {
	// ErrorHandler, when non-nil, is called instead of the default JSON error
	// envelope when a request fails. status is the suggested HTTP status code
	// (400 or 500). Implementations must write the response header and body.
	ErrorHandler func(w http.ResponseWriter, r *http.Request, status int, err error)

	// Observer, when non-nil, receives per-request lifecycle events: request
	// counts with latency and HTTP status, and per-field validation errors.
	// Defaults to [stats.NoopObserver] when nil.
	Observer stats.Observer

	// MaxBodyBytes limits the number of bytes read from the request body for
	// body-bearing methods (POST, PUT, PATCH). Zero means the default (1 MiB).
	// Requests exceeding the limit are rejected with 400 Bad Request.
	MaxBodyBytes int64

	// ContentType is the expected Content-Type for body-bearing methods (POST,
	// PUT, PATCH). When non-empty, requests whose Content-Type does not match
	// (ignoring parameters such as "; charset=utf-8") are rejected with
	// 415 Unsupported Media Type. Defaults to "application/json".
	ContentType string

	// MultiValueQueryParams, when true, passes the raw multi-value query map
	// (map[string][]string from r.URL.Query()) to [rest.RouteHandle.ValidateQueryMulti]
	// instead of the flat single-value map. Use when your routes use repeated query
	// keys such as "?tags=a&tags=b". When false (default), the first value per key
	// is validated via [rest.RouteHandle.ValidateQuery].
	MultiValueQueryParams bool

	// SecurityFunc, when non-nil, is called for routes that declare a non-nil
	// Security field (via [rest.RouteMeta.Security] or global security), after
	// parameter validation but before the handler fn.
	//
	// Return a non-nil error to reject the request with 401 Unauthorized.
	// reqs contains the route's declared security requirements (scheme names +
	// scopes). The adapter has already extracted and codec-validated the credential
	// from the request before calling SecurityFunc.
	//
	// Example — JWT bearer verification:
	//
	//	opts.SecurityFunc = func(ctx context.Context, r *http.Request, reqs []route.SecurityRequirement) error {
	//	    token := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
	//	    return jwtlib.VerifyScopes(token, reqs)
	//	}
	SecurityFunc func(ctx context.Context, r *http.Request, reqs []route.SecurityRequirement) error
}

Options configures the behaviour of Handler and Register.

type PendingCookie added in v0.8.0

type PendingCookie struct {
	Name  string
	Value string
	Opts  CookieOptions
}

PendingCookie is a cookie queued to be validated and written as a Set-Cookie response header by Handler. Create one inside a HandlerFunc and deposit it via WithResponseCookies.

func ResponseCookiesFromContext added in v0.8.0

func ResponseCookiesFromContext(ctx context.Context) ([]PendingCookie, bool)

ResponseCookiesFromContext retrieves pending cookies previously stored by WithResponseCookies. Returns false if no cookies were queued.

type RequestBuildError added in v0.10.0

type RequestBuildError struct {
	// Err is the underlying error from [http.NewRequestWithContext].
	Err error
}

RequestBuildError is returned by Call when constructing the outgoing *http.Request fails (e.g. malformed base URL or context already cancelled).

Use errors.As to extract the underlying error for slog logging:

var buildErr nethttp.RequestBuildError
if errors.As(err, &buildErr) {
    slog.Error("failed to build request", "cause", buildErr.Err)
}

func (RequestBuildError) Error added in v0.10.0

func (e RequestBuildError) Error() string

func (RequestBuildError) Unwrap added in v0.10.0

func (e RequestBuildError) Unwrap() error

Unwrap allows errors.Is and errors.As to traverse the underlying error.

type RequestError added in v0.10.0

type RequestError struct {
	// Method is the HTTP method (e.g. "GET", "POST").
	Method string
	// Path is the route path template (e.g. "/users/{id}").
	Path string
	// Err is the underlying transport error from [http.Client.Do].
	Err error
}

RequestError is returned by Call when executing the HTTP call fails (network error, DNS failure, TLS error, or context cancellation).

Use errors.As to extract the structured fields for slog logging:

var reqErr nethttp.RequestError
if errors.As(err, &reqErr) {
    slog.Error("http call failed",
        "method", reqErr.Method,
        "path",   reqErr.Path,
        "cause",  reqErr.Err,
    )
}

func (RequestError) Error added in v0.10.0

func (e RequestError) Error() string

func (RequestError) Unwrap added in v0.10.0

func (e RequestError) Unwrap() error

Unwrap allows errors.Is and errors.As to traverse the underlying error.

type ResponseBodyError added in v0.10.0

type ResponseBodyError struct {
	// Err is the underlying error from reading the response body.
	Err error
}

ResponseBodyError is returned by Call when reading the HTTP response body fails after a successful connection.

Use errors.As to extract the underlying error for slog logging:

var bodyErr nethttp.ResponseBodyError
if errors.As(err, &bodyErr) {
    slog.Error("failed to read response body", "cause", bodyErr.Err)
}

func (ResponseBodyError) Error added in v0.10.0

func (e ResponseBodyError) Error() string

func (ResponseBodyError) Unwrap added in v0.10.0

func (e ResponseBodyError) Unwrap() error

Unwrap allows errors.Is and errors.As to traverse the underlying error.

type SSEHandlerFunc added in v0.8.0

type SSEHandlerFunc[Req, Event any] func(ctx context.Context, req Req, send func(Event) error) error

SSEHandlerFunc is the typed application handler called by SSEHandler. ctx is the request context (cancelled when the client disconnects). req is the decoded request (zero value for body-less GET requests). send encodes, validates, and writes one SSE event; it returns an error if the event fails codec validation or if the underlying write fails.

type UnexpectedStatusError added in v0.10.0

type UnexpectedStatusError struct {
	// Method is the HTTP method used for the request (e.g. "GET", "POST").
	Method string
	// Path is the route path template, not the concrete URL (e.g. "/users/{id}").
	// Use this for log grouping and metrics — it does not contain the base URL or
	// concrete path variable values.
	Path string
	// StatusCode is the HTTP response status code returned by the server.
	StatusCode int
	// Body is the raw response body returned by the server (may be nil or empty).
	Body []byte
}

UnexpectedStatusError is returned by Call when the server responds with a non-2xx HTTP status code.

Use errors.As to extract the structured fields for slog logging:

var statusErr nethttp.UnexpectedStatusError
if errors.As(err, &statusErr) {
    slog.Error("unexpected response",
        "method", statusErr.Method,
        "path",   statusErr.Path,
        "status", statusErr.StatusCode,
        "body",   string(statusErr.Body),
    )
}

func (UnexpectedStatusError) Error added in v0.10.0

func (e UnexpectedStatusError) Error() string

Jump to

Keyboard shortcuts

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