Skip to main content

Crate seiva_http

Crate seiva_http 

Source
Expand description

Delonix Seiva — the HTTP transport.

This crate provides the transport-level pieces a module’s generated controller builds on. It is demo-free and DORM-free: it links no example module and no data layer, so it is a publishable building block. A module’s #[seiva::controller] router and the composition root (seiva-server or a project’s apps/<name>-api) merge the module routes into router.

router assembles the framework surface — health, /openapi.json (generated from every facade linked into the binary) and /docs — wrapped in the security headers, optional CORS, the body-size limit, the rate limiter and the request-id middleware. It mounts no module route itself; the composition root merges those in.

Re-exports§

pub use audit::audit;
pub use authn::authn;
pub use authn::AuthConfig;
pub use authn::Authn;
pub use backpressure::with_route_controls;
pub use backpressure::RouteControls;
pub use backpressure::RouteLimit;
pub use bus::Authorize;
pub use bus::Idempotent;
pub use bus::DEFAULT_IDEMPOTENCY_TTL;
pub use ctx::Ctx;
pub use docs::docs_handler;
pub use docs::redoc_handler;
pub use docs::scalar_handler;
pub use docs::DOCS_HTML;
pub use guard::permission_guard;
pub use guard::require_permission;
pub use guard::PrincipalExt;
pub use guard::RequiredPermission;
pub use guard::Scoped;
pub use health::health_router;
pub use health::health_router_with_dependency;
pub use health::Health;
pub use health::HealthState;
pub use health::ReadinessCheck;
pub use idempotency::fingerprint;
pub use idempotency::in_progress_problem;
pub use idempotency::mismatch_problem;
pub use idempotency::scoped_key;
pub use idempotency::IdempotencyKey;
pub use idempotency::OptionalIdempotencyKey;
pub use idempotency::IDEMPOTENCY_HEADER;
pub use idempotency::MAX_KEY_LEN;
pub use metrics::track_metrics;
pub use metrics::REQUESTS_IN_FLIGHT;
pub use metrics::REQUESTS_TOTAL;
pub use metrics::REQUEST_DURATION_SECONDS;
pub use problem::ProblemResponse;
pub use problem::PROBLEM_JSON;
pub use rate_limit::rate_limit;
pub use rate_limit::rate_limit_with_store;
pub use rate_limit::DistributedRateLimit;
pub use rate_limit::RateLimit;
pub use rate_limit::RateLimiter;
pub use request_id::request_id;
pub use request_id::RequestId;
pub use request_id::REQUEST_ID_HEADER;
pub use security::cors_layer;
pub use security::security_headers;
pub use security::HttpSecurity;
pub use valid::Valid;

Modules§

audit
Persistent audit of administrative (and other sensitive) mutations (ADR 0013).
authn
The deny-by-default authentication middleware (ADR 0006).
backpressure
Per-route load control: a request timeout (→ 504 Gateway Timeout) and a concurrency limit with immediate shedding (→ 503 Service Unavailable), applied per route so one slow or heavy endpoint cannot pin every worker while the rest of the service stays responsive.
bus
The two pipeline behaviors that bind to the request context (ADR 0020).
ctx
The framework request context a facade method may receive as &Ctx.
docs
The interactive API-reference pages — Seiva’s FastAPI /docs parity.
guard
The descriptor-driven permission guard.
health
Liveness and readiness endpoints.
idempotency
HTTP-facing building blocks for durable idempotency (ADR 0011).
metrics
Per-request metrics, recorded against the metrics facade (ADR 0027).
problem
RFC 9457 application/problem+json error responses.
rate_limit
A fixed-window, per-client request rate limiter (OWASP API4).
request_id
Per-request correlation id.
security
Secure-by-default response headers and configurable CORS.
valid
A typed, validated JSON body extractor.

Structs§

ApiKeyAuthenticator
An opaque API-key → Principal map with revocation: the X-API-Key counterpart to TokenAuthenticator, sitting behind the same Authenticator trait so a transport treats it like any other bearer path.
AuditEntry
An audit record of a sensitive — typically administrative — mutation (ADR 0013): who did what, to which object, with what result, and a correlation id. Field-level details are redacted of secrets before they ever reach a sink (see AuditEntry::redacted); the full request body with its secrets is never stored.
JwtAuthenticator
Verifies an OIDC/JWT bearer against an injected JWKS and maps its claims to a Principal.
Principal
The authenticated caller: who they are, the tenant their requests are scoped to (if any), and the permissions they hold.
Problem
An RFC 9457 problem detail: a machine-readable description of an error.
TokenAuthenticator
A static bearer-token → Principal map: the first-cut authenticator.
ValidationError
Why a body was rejected as invalid. A single message in this first cut; a later phase can carry a per-field map.

Enums§

AuthError
Why authentication failed. Free of any HTTP type so the trait does not drag a transport into the auth contract; each transport maps the variants to its own status codes.
RateLimitDecision
The outcome of a rate-limit check for one client (ADR 0015).

Constants§

DEFAULT_BODY_LIMIT_BYTES
The default maximum request body size (1 MiB). Oversized bodies are rejected before a handler runs. Mounted as DefaultBodyLimit in router.
DEFAULT_REQUEST_TIMEOUT
The default per-request timeout (30 s). A request whose handler (or whose slow client streaming the body) outlives this is cut off with 408 Request Timeout, so a slowloris client cannot pin a worker indefinitely (OWASP API4). Mounted as a tower_http::timeout::TimeoutLayer in the framework layers.

Traits§

AuditSink
A persistent sink for AuditEntry records (ADR 0013). The base framework codes against this port; an adapter (the DORM gateway) persists the entries.
Authenticator
Turns a presented bearer token into a Principal, or rejects it.
RateLimitStore
A distributed rate-limit backend (ADR 0015): the authority a cluster shares so a client’s budget is enforced across replicas, not per-process.
Validate
A domain-shaped validation check on a deserialized body. Keep it cheap and synchronous: structural and range checks, not I/O. Business rules that need ports belong in the application layer, not here.

Functions§

openapi_document
Builds the service’s OpenAPI 3.1 document from every facade linked into the binary, discovered through seiva::contract::registered_facades (each #[seiva::facade] registers itself in a link-time inventory). Nothing here is hand-written and no central list is kept: a module mounted into the runtime contributes its operations automatically (OWASP API9). This crate links no facade of its own, so a plain seiva-http binary documents nothing; the full runtime (see seiva-server) documents every mounted module.
route_collisions
The (version, resource) route-prefix collisions among every facade linked into the binary — two facades whose generated routers would mount on the same prefix and make Router::merge panic at startup. Empty when the mounted facades are collision-free. A composition root calls this at boot to fail fast with a clear message instead of a bare axum panic (see seiva::contract::route_collisions).
router
Builds the framework router with the default security posture (HttpSecurity::default: secure response headers on, same-origin only — no CORS). Equivalent to router_with_security with the default configuration; the zero-config entrypoint.
router_with_auth
Builds the framework router under security and an optional authentication layer, described by auth (an AuthConfig, or any Option<Arc<dyn Authenticator>> which converts into a bearer-only one). When it carries any authenticator, every request is authenticated (ADR 0006) ahead of any route’s permission_guard — a missing credential becomes Principal::anonymous (public routes still serve, guarded routes 403), a present-but-invalid one is a 401. AuthConfig::none mounts no authn layer (no caller is ever authenticated, so guarded routes answer 403): the composition root supplies an Authenticator to actually admit callers. Pass AuthConfig::with_api_key to also admit opaque X-API-Key callers.
router_with_modules
Builds the full application router: the framework routes plus the caller’s modules, wrapped in the framework layers — the composition a real apps/<name>-api needs.
router_with_modules_distributed
Like router_with_modules, but enforces the per-client rate limit with a distributed store (DistributedRateLimit, ADR 0015) instead of the per-process limiter — the form that holds ONE budget across replicas behind a load balancer. The distributed limiter takes the in-process one’s exact layer position (inside the security headers, so a 429 still carries them), so a 429 keeps its correlation id and headers; security.rate_limit is ignored when a distributed limiter is supplied. Set security.trust_forwarded_for (and pass it through on the limiter) only behind a single trusted proxy.
router_with_security
Builds the framework router — the health endpoints, /openapi.json and /docs. Request flows through the layers outermost-first: request_id() → security_headers → the rate limiter → the optional CORS layer → the body-size limit → the route.