Skip to main content

App

Struct App 

Source
pub struct App { /* private fields */ }
Expand description

Thin wrapper over Router plus server settings and lifecycle hooks.

Implementations§

Source§

impl App

Source

pub fn bind(self, target: impl Into<Bind>) -> BoundApp

Choose a bind target; call .serve() (optionally after .shutdown(...)).

Source

pub async fn listen(self, port: u16) -> Result<()>

Bind 0.0.0.0:port, run CLI if present, otherwise serve.

Prefer this for the common case; use Self::bind for custom addresses. When a plugin attached TLS via [Self::use_tls] (e.g. Acme), this serves HTTPS.

Source§

impl App

Source

pub fn build(&self) -> Result<Server>

Compile routes once into a Server. Prefer this over repeated App::handle.

Source§

impl App

Source

pub fn new() -> Self

Source

pub fn register_cli<F, Fut>(&mut self, name: &'static str, f: F) -> &mut Self
where F: Fn(Arc<StateMap>, Vec<String>) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<()>> + Send + 'static,

Register a plugin CLI command handled by Self::run (e.g. "migrate").

Source

pub fn register_check<F, Fut>(&mut self, name: &'static str, f: F) -> &mut Self
where F: Fn(Arc<StateMap>) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<()>> + Send + 'static,

Register a readiness check (CheckKind::Ready) for GET /ready and CLI check.

Source

pub fn register_audit<F, Fut>(&mut self, name: &'static str, f: F) -> &mut Self
where F: Fn(Arc<StateMap>) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<()>> + Send + 'static,

Register a deploy-time audit (CheckKind::Audit) — CLI check only, not /ready.

Source

pub async fn run_checks( &self, state: Arc<StateMap>, kinds: &[CheckKind], ) -> Vec<CheckResult>

Run registered checks filtered by CheckKind.

Source

pub fn with_probes(&mut self) -> &mut Self

Install k8s-style probes: GET /healthz (liveness) and GET /ready (Ready checks).

Idempotent. Presets (App::web / App::api) call this automatically.

Source

pub fn max_body_size(&mut self, bytes: usize) -> &mut Self

Source

pub fn max_connections(&mut self, n: usize) -> &mut Self

Cap concurrent TCP/UDS connections (default 1024).

Source

pub fn max_upgraded_connections(&mut self, n: usize) -> &mut Self

Cap concurrent HTTP upgrades (WebSocket, …). Excess → 503 + Retry-After.

Source

pub fn max_concurrent_streams(&mut self, n: usize) -> &mut Self

Cap concurrent HTTP/2 streams per connection (default 200). Excess streams → GOAWAY/stream-level rejection handled by hyper.

Source

pub fn max_headers(&mut self, n: usize) -> &mut Self

Max HTTP/1 header count (default 100). Excess → 431 from hyper.

Source

pub fn max_buf_size(&mut self, bytes: usize) -> &mut Self

Cap hyper’s connection buffer (headers + body framing). Minimum 8192. Default ~400 KiB. Use this to bound oversized header blocks.

Source

pub fn request_timeout(&mut self, timeout: Option<Duration>) -> &mut Self

Per-request timeout around the handler (default 30s). None disables.

Measured: timeout ends when the handler returns a Response. Streaming response bodies (SSE) continue afterward and are not cut by this timer. Idle between stream chunks is governed by TCP/keep-alive, not this setting.

Source

pub fn header_read_timeout(&mut self, timeout: Duration) -> &mut Self

Timeout for reading request headers (Slowloris). Also applied while waiting for the next keep-alive request (see Self::idle_timeout). Default 10s.

Source

pub fn idle_timeout(&mut self, timeout: Duration) -> &mut Self

Keep-alive idle: how long a quiet connection may wait for the next request. Hyper uses one timer for header reads; the effective wait is min(header_read_timeout, idle_timeout). Default 60s.

Source

pub fn drain_timeout(&mut self, timeout: Duration) -> &mut Self

How long to wait for in-flight connections after accept stops (default 20s).

Source

pub fn keep_alive(&mut self, enabled: bool) -> &mut Self

HTTP/1 keep-alive (default true).

Source

pub fn trust_proxy(&mut self, trust: bool) -> &mut Self

When true, ClientAddr may use X-Forwarded-For / Forwarded (only behind a trusted proxy).

Source

pub fn cli_mode(&mut self, enabled: bool) -> &mut Self

Mark this app as running under the CLI helper (skips BackgroundServices by default).

Source

pub fn service_in_cli(&mut self, enabled: bool) -> &mut Self

Start BackgroundServices even when Self::cli_mode is set (default false).

Source

pub fn install<P: Plugin>(&mut self, plugin: P) -> &mut Self

Source

pub fn has_plugin(&self, id: &str) -> bool

Whether a plugin with this Plugin::id was already installed.

Source

pub fn installed_plugin_meta(&self) -> &[InstalledPlugin]

Metadata for every plugin passed to Self::install (order preserved).

Source

pub async fn run(self) -> Result<()>

Primary app entrypoint: run as a server process.

CLI mode:

  • check
  • routes
  • plugins
  • openapi --out <path>
  • tasks
  • i18n missing

Non-CLI mode binds via Bind::Env (HOST/PORT, default port 3000). Prefer App::bind + BoundApp::run when the address is fixed in code.

Source

pub fn service<S: BackgroundService + 'static>( &mut self, service: S, ) -> &mut Self

Register a process-local BackgroundService.

Lifecycle: compile → on_startup → services → accept; stop: stop accept → drain → stop services → on_shutdown.

Source

pub fn on_startup<F, Fut>(&mut self, f: F) -> &mut Self
where F: Fn(Arc<StateMap>) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<()>> + Send + 'static,

Run before accepting connections. Err prevents the server from starting.

Source

pub fn on_shutdown<F, Fut>(&mut self, f: F) -> &mut Self
where F: Fn() -> Fut + Send + Sync + 'static, Fut: Future<Output = ()> + Send + 'static,

Run after the accept loop stops, connections drain, and services stop.

Source

pub fn events(&mut self) -> EventBus

Shared EventBus — inserts a default bus into app state on first use.

Source

pub fn explain(&self) -> String

Route map for debugging / startup banner.

Source

pub async fn handle(&self, req: Request) -> Response

Handle one request (compiles the router each call). Prefer Self::build.

Source

pub async fn handle_request( &self, method: Method, path: &str, body: &str, ) -> Response

Sugar over Request::builder + Self::handle (no custom headers). For headers use Request::builder().header(...).build() + Self::handle. Prefer Server::handle_request after Self::build.

Source§

impl App

Source

pub fn configure(&mut self) -> Result<&mut Self>

Load sova.toml or Sova.toml from the current directory, then env overrides.

Missing file is not an error — only SOVA_* env overrides apply.

Source

pub fn configure_from_path( &mut self, path: impl AsRef<Path>, ) -> Result<&mut Self>

Load settings from a toml file (app-level only), then SOVA_* env overrides.

Source

pub fn configure_from_str(&mut self, text: &str) -> Result<&mut Self>

Parse toml and apply [server] (+ legacy) for the active profile, then env overrides.

Source

pub fn from_toml(path: impl AsRef<Path>) -> Result<Self>

Source

pub fn config_doc(&self) -> Option<Arc<ConfigDoc>>

Shared ConfigDoc from the last successful Self::configure_from_str, if any.

Methods from Deref<Target = Router>§

Source

pub fn use_middleware<M>(&mut self, mw: M) -> &mut Self
where M: IntoMwEntry,

Source

pub fn with<T: RouteValue>(&mut self, value: T) -> &mut Self

Attach a RouteValue to the last HTTP route, or to router defaults.

After get/post/…, writes to that route. Otherwise writes to router/app defaults (inherited by routes: route > router > app).

Source

pub fn with_update<T, F>(&mut self, f: F) -> &mut Self
where T: RouteValue + Clone + Default, F: FnOnce(&mut T),

Update a RouteValue on the last route (insert T::default() if missing).

Source

pub fn route_middleware<M>(&mut self, mw: M) -> &mut Self
where M: IntoMwEntry,

Push middleware onto the last registered HTTP route only.

Source

pub fn route_meta<T: RouteValue>(&mut self, value: T) -> &mut Self

Alias for Self::with (writes to the last route when present).

Source

pub fn state<T>(&mut self, value: T) -> &mut Self
where T: Send + Sync + 'static,

Source

pub fn try_state<T>(&self) -> Option<Arc<T>>
where T: Send + Sync + 'static,

Shared app state inserted via Self::state, if present.

Source

pub fn get<H, T>(&mut self, path: &str, handler: H) -> &mut Self
where H: IntoHandler<T>,

Source

pub fn post<H, T>(&mut self, path: &str, handler: H) -> &mut Self
where H: IntoHandler<T>,

Source

pub fn put<H, T>(&mut self, path: &str, handler: H) -> &mut Self
where H: IntoHandler<T>,

Source

pub fn patch<H, T>(&mut self, path: &str, handler: H) -> &mut Self
where H: IntoHandler<T>,

Source

pub fn delete<H, T>(&mut self, path: &str, handler: H) -> &mut Self
where H: IntoHandler<T>,

Source

pub fn head<H, T>(&mut self, path: &str, handler: H) -> &mut Self
where H: IntoHandler<T>,

Explicit HEAD handler (takes precedence over GET→strip-body).

Source

pub fn options<H, T>(&mut self, path: &str, handler: H) -> &mut Self
where H: IntoHandler<T>,

Explicit OPTIONS handler (takes precedence over auto 204 + Allow).

Source

pub fn redirect( &mut self, from: &str, to: impl Into<String>, status: u16, ) -> &mut Self

GET from → redirect to to with the given HTTP status (e.g. 302, 301, 303).

app.redirect("/health", "/healthz", 302);
app.redirect("/old", "/new", 301);
Source

pub fn raw<H>(&mut self, path: &str, handler: H) -> &mut Self
where H: IntoRawHandler,

Escape hatch: handle a path with a raw Hyper request/response (no Sova middleware).

Source

pub fn mount(&mut self, prefix: &str, other: Router) -> &mut Self

Mount a child router under prefix.

Bakes the child’s middleware stack into its routes. Does not prepend this router’s middleware — the eventual root stack is applied once in compile_router as an outer wrap (so App-level middleware is not doubled).

Source

pub fn group<F>(&mut self, prefix: &str, f: F) -> &mut Self
where F: FnOnce(&mut Router),

Sugar over Self::mount: build a child router in a closure.

Source

pub fn catch<H, T>(&mut self, status: u16, handler: H) -> &mut Self
where H: IntoHandler<T>,

Register a catcher for HTTP status in this router’s mount scope.

At dispatch, the catcher with the longest matching prefix wins. not_found is sugar for catch(404, …).

Source

pub fn not_found<H, T>(&mut self, handler: H) -> &mut Self
where H: IntoHandler<T>,

Sugar for Self::catch(404, handler).

Source

pub fn error_handler<F, Fut>(&mut self, f: F) -> &mut Self
where F: Fn(Error) -> Fut + Send + Sync + 'static, Fut: Future<Output = Response> + Send + 'static,

Called when a leaf handler returns Err. Request is already consumed.

Source

pub fn route_entries(&self) -> Vec<RouteEntry>

Full introspection: HTTP routes and raw paths.

Source

pub fn explain(&self) -> String

Human-readable route map (method, path, middleware names).

Trait Implementations§

Source§

impl Default for App

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Deref for App

Source§

type Target = Router

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Router

Dereferences the value.
Source§

impl DerefMut for App

Source§

fn deref_mut(&mut self) -> &mut Router

Mutably dereferences the value.

Auto Trait Implementations§

§

impl !RefUnwindSafe for App

§

impl !Sync for App

§

impl !UnwindSafe for App

§

impl Freeze for App

§

impl Send for App

§

impl Unpin for App

§

impl UnsafeUnpin for App

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more