pub struct ServerBuilder { /* private fields */ }Expand description
Builder for configuring and launching the HTTP server.
§Example
use stateset_embedded::Commerce;
use stateset_http::ServerBuilder;
let commerce = Commerce::new(":memory:")?;
ServerBuilder::new_from_env(commerce)?
.bind("0.0.0.0:8080".parse()?)
.with_cors()
.with_request_id()
.with_bearer_auth("replace-me-with-a-secret")
.serve()
.await?;Implementations§
Source§impl ServerBuilder
impl ServerBuilder
Sourcepub fn new(commerce: Commerce) -> Self
pub fn new(commerce: Commerce) -> Self
Create a new server builder wrapping a Commerce instance.
Sourcepub fn new_from_env(commerce: Commerce) -> Result<Self, HttpError>
pub fn new_from_env(commerce: Commerce) -> Result<Self, HttpError>
Create a new server builder and apply /metrics network policy from environment.
This is useful for startup wiring where operators configure network policy via:
STATESET_HTTP_METRICS_IP_ALLOWLISTSTATESET_HTTP_METRICS_IP_CIDR_ALLOWLISTSTATESET_HTTP_METRICS_TRUSTED_PROXIESSTATESET_HTTP_METRICS_FORWARDED_MAX_BYTESSTATESET_HTTP_METRICS_X_FORWARDED_FOR_MAX_BYTESSTATESET_HTTP_METRICS_X_REAL_IP_MAX_BYTESSTATESET_HTTP_METRICS_AUTHORIZATION_MAX_BYTESSTATESET_HTTP_ALLOW_UNAUTHENTICATED
Sourcepub fn with_allow_unauthenticated_from_env(self) -> Result<Self, HttpError>
pub fn with_allow_unauthenticated_from_env(self) -> Result<Self, HttpError>
Apply the unauthenticated opt-out from STATESET_HTTP_ALLOW_UNAUTHENTICATED.
Accepts true/false (also 1/0, yes/no). Unset or empty leaves
the secure default (false) in place.
Sourcepub const fn require_idempotency_keys(self, require: bool) -> Self
pub const fn require_idempotency_keys(self, require: bool) -> Self
Require an Idempotency-Key header on money-moving create endpoints
(POST /orders, /payments, /payments/{id}/refund, /ap/payments),
returning HTTP 428 (Precondition Required) when it is missing.
Defaults to true; pass false to let existing deployments opt out.
Sourcepub fn with_require_idempotency_keys_from_env(self) -> Result<Self, HttpError>
pub fn with_require_idempotency_keys_from_env(self) -> Result<Self, HttpError>
Apply the required-idempotency-key opt-out from
STATESET_HTTP_REQUIRE_IDEMPOTENCY_KEYS.
Accepts true/false (also 1/0, yes/no). Unset or empty leaves
the secure default (true) in place.
Sourcepub const fn bind(self, addr: SocketAddr) -> Self
pub const fn bind(self, addr: SocketAddr) -> Self
Set the bind address.
Sourcepub const fn with_cors(self) -> Self
pub const fn with_cors(self) -> Self
Enable CORS middleware with explicit defaults and optional env override.
Set STATESET_HTTP_ALLOWED_ORIGINS to a comma-separated list of origins
to override the localhost-only defaults.
Sourcepub const fn with_request_id(self) -> Self
pub const fn with_request_id(self) -> Self
Enable request-ID middleware (generates UUID, propagates in headers).
Sourcepub fn with_bearer_auth(self, token: impl Into<String>) -> Self
pub fn with_bearer_auth(self, token: impl Into<String>) -> Self
Configure bearer authentication for /api/v1/* endpoints.
Requests to API routes must include Authorization: Bearer <token>.
Sourcepub fn with_metrics_bearer_auth(self, token: impl Into<String>) -> Self
pub fn with_metrics_bearer_auth(self, token: impl Into<String>) -> Self
Configure bearer authentication for /metrics.
Sourcepub fn without_metrics_auth(self) -> Self
pub fn without_metrics_auth(self) -> Self
Disable authentication for /metrics.
Sourcepub fn with_metrics_ip_allowlist<I>(self, ips: I) -> Selfwhere
I: IntoIterator<Item = IpAddr>,
pub fn with_metrics_ip_allowlist<I>(self, ips: I) -> Selfwhere
I: IntoIterator<Item = IpAddr>,
Configure a client IP allowlist for /metrics.
Sourcepub fn without_metrics_ip_allowlist(self) -> Self
pub fn without_metrics_ip_allowlist(self) -> Self
Disable /metrics IP allowlist checks.
Sourcepub fn with_metrics_ip_cidr_allowlist<I>(self, cidrs: I) -> Selfwhere
I: IntoIterator<Item = IpCidr>,
pub fn with_metrics_ip_cidr_allowlist<I>(self, cidrs: I) -> Selfwhere
I: IntoIterator<Item = IpCidr>,
Configure a CIDR-based client IP allowlist for /metrics.
Sourcepub fn without_metrics_ip_cidr_allowlist(self) -> Self
pub fn without_metrics_ip_cidr_allowlist(self) -> Self
Disable CIDR-based /metrics IP allowlist checks.
Sourcepub fn with_metrics_trusted_proxies<I>(self, cidrs: I) -> Selfwhere
I: IntoIterator<Item = IpCidr>,
pub fn with_metrics_trusted_proxies<I>(self, cidrs: I) -> Selfwhere
I: IntoIterator<Item = IpCidr>,
Configure trusted proxy CIDRs for /metrics.
Sourcepub fn with_metrics_header_limits(self, limits: MetricsHeaderLimits) -> Self
pub fn with_metrics_header_limits(self, limits: MetricsHeaderLimits) -> Self
Configure max accepted forwarding header lengths for /metrics.
Sourcepub fn with_metrics_network_policy_from_env(self) -> Result<Self, HttpError>
pub fn with_metrics_network_policy_from_env(self) -> Result<Self, HttpError>
Apply /metrics network policy from environment variables.
Supported variables (comma-separated entries):
STATESET_HTTP_METRICS_IP_ALLOWLIST(exact IPs)STATESET_HTTP_METRICS_IP_CIDR_ALLOWLIST(CIDRs and/or IPs)STATESET_HTTP_METRICS_TRUSTED_PROXIES(CIDRs and/or IPs)
If a variable is present but empty, the corresponding config is disabled.
Sourcepub fn with_metrics_header_limits_from_env(self) -> Result<Self, HttpError>
pub fn with_metrics_header_limits_from_env(self) -> Result<Self, HttpError>
Apply /metrics forwarding header limits from environment variables.
Supported variables (positive integers in bytes):
STATESET_HTTP_METRICS_FORWARDED_MAX_BYTESSTATESET_HTTP_METRICS_X_FORWARDED_FOR_MAX_BYTESSTATESET_HTTP_METRICS_X_REAL_IP_MAX_BYTESSTATESET_HTTP_METRICS_AUTHORIZATION_MAX_BYTES
Sourcepub fn with_request_body_limit_from_env(self) -> Result<Self, HttpError>
pub fn with_request_body_limit_from_env(self) -> Result<Self, HttpError>
Apply the request body size limit from STATESET_HTTP_MAX_BODY_BYTES.
Sourcepub fn without_metrics_trusted_proxies(self) -> Self
pub fn without_metrics_trusted_proxies(self) -> Self
Disable trusted proxy checks for /metrics.
Sourcepub fn bind_auth_tenant(self, tenant_id: impl Into<String>) -> Self
pub fn bind_auth_tenant(self, tenant_id: impl Into<String>) -> Self
Bind the configured bearer token to a single tenant.
Requests using this token must present the same x-tenant-id.
Sourcepub fn bind_auth_actor(self, actor_id: impl Into<String>) -> Self
pub fn bind_auth_actor(self, actor_id: impl Into<String>) -> Self
Bind the configured bearer token to a single actor.
When set, authenticated API requests are treated as this actor during
authorization checks, and any conflicting x-actor-id header is rejected.
Sourcepub fn with_bearer_auth_for_tenant(
self,
token: impl Into<String>,
tenant_id: impl Into<String>,
) -> Self
pub fn with_bearer_auth_for_tenant( self, token: impl Into<String>, tenant_id: impl Into<String>, ) -> Self
Configure bearer authentication and bind it to a tenant in one call.
Sourcepub fn with_bearer_auth_for_actor(
self,
token: impl Into<String>,
actor_id: impl Into<String>,
) -> Self
pub fn with_bearer_auth_for_actor( self, token: impl Into<String>, actor_id: impl Into<String>, ) -> Self
Configure bearer authentication and bind it to an actor in one call.
Sourcepub fn add_bearer_auth(self, token: impl Into<String>) -> Self
pub fn add_bearer_auth(self, token: impl Into<String>) -> Self
Add an additional bearer token for /api/v1/* endpoints.
This leaves the primary configured API token unchanged.
Sourcepub fn add_bearer_auth_for_tenant(
self,
token: impl Into<String>,
tenant_id: impl Into<String>,
) -> Self
pub fn add_bearer_auth_for_tenant( self, token: impl Into<String>, tenant_id: impl Into<String>, ) -> Self
Add an additional bearer token bound to a tenant.
Sourcepub fn add_bearer_auth_for_actor(
self,
token: impl Into<String>,
actor_id: impl Into<String>,
) -> Self
pub fn add_bearer_auth_for_actor( self, token: impl Into<String>, actor_id: impl Into<String>, ) -> Self
Add an additional bearer token bound to an actor.
Sourcepub fn add_bearer_auth_for_actor_and_tenant(
self,
token: impl Into<String>,
actor_id: impl Into<String>,
tenant_id: impl Into<String>,
) -> Self
pub fn add_bearer_auth_for_actor_and_tenant( self, token: impl Into<String>, actor_id: impl Into<String>, tenant_id: impl Into<String>, ) -> Self
Add an additional bearer token bound to both an actor and a tenant.
Sourcepub fn with_tenant_db_dir(self, base_dir: impl Into<PathBuf>) -> Self
pub fn with_tenant_db_dir(self, base_dir: impl Into<PathBuf>) -> Self
Enable per-tenant storage using <base_dir>/<tenant>.db.
Sourcepub fn with_ignore_tenant_header(self) -> Self
pub fn with_ignore_tenant_header(self) -> Self
Ignore x-tenant-id headers when per-tenant routing is disabled.
By default such requests are rejected with 400 instead of silently
being served shared data. Use this escape hatch only for deployments
that intentionally front a multi-tenant proxy which handles tenant
isolation upstream.
Sourcepub fn with_max_tenant_dbs(self, max_tenant_dbs: usize) -> Self
pub fn with_max_tenant_dbs(self, max_tenant_dbs: usize) -> Self
Set the maximum number of lazily created tenant databases kept in-memory.
Sourcepub const fn with_max_request_body_bytes(
self,
max_request_body_bytes: usize,
) -> Self
pub const fn with_max_request_body_bytes( self, max_request_body_bytes: usize, ) -> Self
Set the maximum accepted request body size for extractor-based endpoints.
Sourcepub fn with_authz_engine(self, engine: AuthzEngine) -> Self
pub fn with_authz_engine(self, engine: AuthzEngine) -> Self
Enable request authorization for /api/v1/* using a provided authz engine.
By default, authorization expects actor identity to come from actor-bound
bearer tokens. If you want to trust x-actor-id request headers instead,
also call Self::trust_actor_headers_for_authz.
Sourcepub const fn trust_actor_headers_for_authz(self) -> Self
pub const fn trust_actor_headers_for_authz(self) -> Self
Explicitly trust x-actor-id request headers for authorization.
Use this only behind a trusted upstream that authenticates callers and strips or overwrites any client-supplied actor header.
Sourcepub const fn allow_unauthenticated(self) -> Self
pub const fn allow_unauthenticated(self) -> Self
Explicitly opt out of the fail-closed authentication startup check.
By default the server refuses to start on a non-loopback bind address
when no API bearer tokens are configured. Call this (or set
STATESET_HTTP_ALLOW_UNAUTHENTICATED=true) to acknowledge the risk and
serve unauthenticated API traffic anyway.
Sourcepub const fn with_strict_authz(self) -> Self
pub const fn with_strict_authz(self) -> Self
Fail closed on authorization for unmapped API paths.
By default, /api/v1 paths that the authorization layer cannot map to
a resource/action pair bypass authorization (authentication still
applies). When strict mode is enabled, such requests are denied with
HTTP 403 instead. Only takes effect when authorization is configured
via Self::with_authz_engine.
Sourcepub fn without_auth(self) -> Self
pub fn without_auth(self) -> Self
Disable API authentication (not recommended for untrusted networks).
Sourcepub const fn with_rate_limit(
self,
requests_per_second: u64,
burst_size: u64,
) -> Self
pub const fn with_rate_limit( self, requests_per_second: u64, burst_size: u64, ) -> Self
Enable global rate limiting with a token bucket.
requests_per_second controls steady-state throughput; burst_size
controls how many requests can be served in a burst before throttling.
Requests exceeding the limit receive HTTP 429.
Sourcepub fn bearer_auth_token(&self) -> Option<&str>
pub fn bearer_auth_token(&self) -> Option<&str>
Return the configured bearer token, if auth is enabled.
Sourcepub fn metrics_bearer_auth_token(&self) -> Option<&str>
pub fn metrics_bearer_auth_token(&self) -> Option<&str>
Return the configured bearer token for /metrics, if enabled.
Sourcepub fn metrics_ip_allowlist(&self) -> Option<Vec<IpAddr>>
pub fn metrics_ip_allowlist(&self) -> Option<Vec<IpAddr>>
Return configured metrics IP allowlist entries, if any.
Sourcepub fn metrics_ip_cidr_allowlist(&self) -> Option<Vec<IpCidr>>
pub fn metrics_ip_cidr_allowlist(&self) -> Option<Vec<IpCidr>>
Return configured CIDR-based metrics IP allowlist entries, if any.
Sourcepub fn metrics_trusted_proxies(&self) -> Option<Vec<IpCidr>>
pub fn metrics_trusted_proxies(&self) -> Option<Vec<IpCidr>>
Return configured trusted proxy CIDRs for /metrics, if any.
Sourcepub const fn metrics_header_limits(&self) -> MetricsHeaderLimits
pub const fn metrics_header_limits(&self) -> MetricsHeaderLimits
Return configured max accepted forwarding header lengths for /metrics.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for ServerBuilder
impl !UnwindSafe for ServerBuilder
impl Freeze for ServerBuilder
impl Send for ServerBuilder
impl Sync for ServerBuilder
impl Unpin for ServerBuilder
impl UnsafeUnpin for ServerBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ServiceExt for T
impl<T> ServiceExt for T
Source§fn compression(self) -> Compression<Self>where
Self: Sized,
fn compression(self) -> Compression<Self>where
Self: Sized,
compression-br or compression-deflate or compression-gzip or compression-zstd only.Source§fn trace_for_http(self) -> Trace<Self, SharedClassifier<ServerErrorsAsFailures>>where
Self: Sized,
fn trace_for_http(self) -> Trace<Self, SharedClassifier<ServerErrorsAsFailures>>where
Self: Sized,
trace only.Source§fn trace_for_grpc(self) -> Trace<Self, SharedClassifier<GrpcErrorsAsFailures>>where
Self: Sized,
fn trace_for_grpc(self) -> Trace<Self, SharedClassifier<GrpcErrorsAsFailures>>where
Self: Sized,
trace only.Source§fn follow_redirects(self) -> FollowRedirect<Self>where
Self: Sized,
fn follow_redirects(self) -> FollowRedirect<Self>where
Self: Sized,
follow-redirect only.Source§fn set_request_id<M>(
self,
header_name: HeaderName,
make_request_id: M,
) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
fn set_request_id<M>(
self,
header_name: HeaderName,
make_request_id: M,
) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
request-id only.Source§fn set_x_request_id<M>(self, make_request_id: M) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
fn set_x_request_id<M>(self, make_request_id: M) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
request-id only.x-request-id as the header name. Read moreSource§fn propagate_request_id(
self,
header_name: HeaderName,
) -> PropagateRequestId<Self>where
Self: Sized,
fn propagate_request_id(
self,
header_name: HeaderName,
) -> PropagateRequestId<Self>where
Self: Sized,
request-id only.Source§fn propagate_x_request_id(self) -> PropagateRequestId<Self>where
Self: Sized,
fn propagate_x_request_id(self) -> PropagateRequestId<Self>where
Self: Sized,
request-id only.x-request-id as the header name. Read more