api

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: MIT Imports: 39 Imported by: 0

Documentation

Overview

Package api assembles Huma on Go's HTTP server with shared request policies.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Authenticated

func Authenticated(a huma.API, authenticator Authenticator) huma.API

Authenticated keeps bearer enforcement and its OpenAPI security declaration together.

func Bearer

func Bearer(authenticator Authenticator) func(huma.Context, func(huma.Context))

Bearer only accepts the Authorization header. Nil authentication fails closed. A database outage remains an operational error, never anonymous access.

func ClientIP

func ClientIP(ctx context.Context) (string, error)

ClientIP returns the address accepted at the HTTP boundary. With no trusted proxies it is the immediate peer. It is a rate-limit signal, not a principal.

func ClientIPKey

func ClientIPKey(c huma.Context) (string, error)

ClientIPKey uses the address accepted by the server's trusted-proxy policy. Keep the key namespace stable for direct deployments upgrading this policy.

func CurrentUserID

func CurrentUserID(ctx context.Context) (uuid.UUID, error)

CurrentUserID returns the authenticated account ID without loading the account.

func Multipart

func Multipart(ctx huma.Context, next func(huma.Context))

Multipart bounds the complete request, spools files above 1 MiB to disk, and removes temporary files after Huma and the handler return, including on failure. Use RawBody multipart.Form and set the operation's MaxBodyBytes explicitly. Register after authentication; this middleware is the sole multipart reader.

func ParseTrustedProxies

func ParseTrustedProxies(cidrs []string) ([]netip.Prefix, error)

ParseTrustedProxies validates explicit IPv4/IPv6 CIDRs. Trusting all addresses would let arbitrary clients supply their own security rate-limit identity.

func PeerIP

func PeerIP(ctx context.Context) (string, error)

PeerIP returns the immediate peer captured by the HTTP boundary. Forwarding headers cannot change this value; trusted ingress needs an explicit policy.

func PeerIPKey

func PeerIPKey(c huma.Context) (string, error)

PeerIPKey always uses the direct connection. Use ClientIPKey for trusted ingress. Arbitrary forwarding headers cannot change the budget. NAT clients share a limit.

func Principal

func Principal(ctx context.Context) (auth.Principal, error)

func RateLimit

func RateLimit(limiter ratelimit.Limiter, limit ratelimit.Limit, key func(huma.Context) (string, error)) func(huma.Context, func(huma.Context))

RateLimit applies one policy to a route group. Keys come from an explicit caller function; raw credentials and personal data should never be used as keys.

func RegisterHealthRoutes

func RegisterHealthRoutes(router *http.ServeMux, ready, stopping <-chan struct{}, check func(context.Context) error)

RegisterHealthRoutes mounts unversioned health endpoints. App owns the signals: ready closes after startup; stopping closes before drain. Nil signals stay pending. Optional check must honor its context and impose a deadline. Only one check runs at a time; concurrent probes return 503 instead of adding dependency load. Dependency failures are deliberately not exposed in the response.

func RequireCurrentUser

func RequireCurrentUser(ctx context.Context, id uuid.UUID) error

RequireCurrentUser requires id to match the authenticated account. A mismatch returns not found; the check grants no administrative rights.

Types

type Authenticator

type Authenticator interface {
	Authenticate(ctx context.Context, token string) (auth.Principal, error)
}

type Options

type Options struct {
	Title, Version string
	// APIVersion identifies the business API in X-API-Version (default v1).
	// Version above identifies the application release in the OpenAPI document.
	APIVersion string
	// DisableDocs hides both /docs and /openapi.json. They are enabled by default.
	DisableDocs bool
	Tracer      trace.Tracer
	Meter       metric.Meter
	// TrustTraceContext accepts remote trace parents from a trusted ingress.
	TrustTraceContext bool
	// TrustedProxies may supply X-Forwarded-For. Empty trusts no forwarding headers.
	// The deployment must secure these proxy connections and sanitize the chain.
	TrustedProxies []netip.Prefix
	Middleware     []func(http.Handler) http.Handler
}

Options configures a server without starting a listener. Zero values are usable. Middleware contains standard net/http middleware and runs inside observability.

type PaginationInput

type PaginationInput struct {
	Page    int `query:"page" default:"1" minimum:"1"`
	PerPage int `query:"per_page" default:"20" minimum:"1" maximum:"100"`
}

PaginationInput binds and validates pagination once, including ambiguous queries.

func (PaginationInput) Pagination

func (in PaginationInput) Pagination() pagination.Pagination

func (*PaginationInput) Resolve

func (in *PaginationInput) Resolve(ctx huma.Context) []error

type Server

type Server struct {
	huma.API
	Mux *http.ServeMux
	// contains filtered or unexported fields
}

Server is a Huma API and an http.Handler. Mux accepts infrastructure handlers; modules register typed operations on the embedded API or native Huma groups. Finish all registration before serving. The application owns listener lifecycle.

func New

func New(options Options) (*Server, error)

New creates an API with safe defaults and live documentation. It starts no I/O.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

Directories

Path Synopsis
Package response constructs typed Huma outputs and safe HTTP problem details.
Package response constructs typed Huma outputs and safe HTTP problem details.

Jump to

Keyboard shortcuts

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