Skip to main content

App

Struct App 

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

A configured web application.

The App holds all routes, middleware, state, and lifecycle hooks, and provides methods to handle incoming requests.

Implementations§

Source§

impl App

Source

pub fn builder() -> AppBuilder

Creates a new application builder.

Source

pub fn test_client(self: Arc<Self>) -> TestClient<Arc<Self>>

Create an in-process test client for this app.

This takes Arc<Self> so tests can keep using shared Arc<App> values without extra boilerplate.

Source

pub fn test_client_with_seed( self: Arc<Self>, seed: u64, ) -> TestClient<Arc<Self>>

Create an in-process test client with a deterministic seed.

Source

pub fn config(&self) -> &AppConfig

Returns the application configuration.

Source

pub fn route_count(&self) -> usize

Returns the number of registered routes.

Source

pub fn websocket_route_count(&self) -> usize

Returns the number of registered websocket routes.

Source

pub fn has_websocket_route(&self, path: &str) -> bool

Returns true if a websocket route matches the given path.

Source

pub fn routes(&self) -> impl Iterator<Item = (Method, &str)>

Returns an iterator over route metadata (method, path).

This is useful for generating OpenAPI specifications or debugging.

Source

pub fn openapi_spec(&self) -> Option<&str>

Returns the generated OpenAPI specification JSON, if OpenAPI is enabled.

§Example
let app = App::builder()
    .openapi(OpenApiConfig::new().title("My API"))
    .build();

if let Some(spec) = app.openapi_spec() {
    println!("OpenAPI spec: {}", spec);
}
Source

pub fn state(&self) -> &Arc<StateContainer>

Returns the shared state container.

Source

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

Gets a reference to shared state of type T.

Source

pub fn exception_handlers(&self) -> &Arc<ExceptionHandlers>

Returns the exception handlers registry.

Source

pub fn override_dependency_value<T>(&self, value: T)
where T: FromDependency,

Register a fixed override value for a dependency type.

This is a test-focused convenience API; production code typically wires dependencies via crate::dependency::FromDependency implementations.

Source

pub fn clear_dependency_overrides(&self)

Clear all registered dependency overrides.

Source

pub fn dependency_overrides(&self) -> Arc<DependencyOverrides>

Returns the shared dependency overrides registry.

Source

pub fn take_background_tasks(req: &mut Request) -> Option<BackgroundTasks>

Take request-scoped background tasks (if any) from a request.

The HTTP server calls this after writing the response so any deferred work runs outside the main request handler path.

Source

pub fn handle_error<E>(&self, ctx: &RequestContext, err: E) -> Option<Response>
where E: Error + Send + Sync + 'static,

Handles an error using registered exception handlers.

If a handler is registered for the error type, it will be invoked. Otherwise, returns None.

Source

pub fn handle_error_or_default<E>( &self, ctx: &RequestContext, err: E, ) -> Response
where E: Error + Send + Sync + 'static,

Handles an error, returning a 500 response if no handler is registered.

Source

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

Handles an incoming request.

This matches the request against registered routes, runs middleware, and returns the response.

Source

pub async fn handle_websocket( &self, ctx: &RequestContext, req: &mut Request, ws: WebSocket, ) -> Result<(), WebSocketError>

Handles an incoming websocket upgrade request after the handshake has been accepted.

The HTTP server is responsible for validating the upgrade headers and writing the 101 response. This function only performs path matching and calls the websocket handler.

Source

pub async fn run_startup_hooks(&self) -> StartupOutcome

Runs all startup hooks.

Hooks run in registration order (FIFO). If a hook returns an error with abort: true, execution stops and returns StartupOutcome::Aborted.

This consumes the startup hooks - they can only be run once.

§Returns
  • StartupOutcome::Success if all hooks succeeded
  • StartupOutcome::PartialSuccess if some hooks had non-fatal errors
  • StartupOutcome::Aborted if a fatal hook error occurred
Source

pub async fn run_shutdown_hooks(&self)

Runs all shutdown hooks.

Hooks run in reverse registration order (LIFO). Errors are logged but do not stop other hooks from running.

This consumes the shutdown hooks - they can only be run once.

Source

pub fn transfer_shutdown_hooks(&self, controller: &ShutdownController)

Transfers shutdown hooks to a ShutdownController.

This moves all registered shutdown hooks to the controller, which will run them during the appropriate shutdown phase.

Call this when integrating with the server’s shutdown mechanism.

Source

pub fn pending_startup_hooks(&self) -> usize

Returns the number of pending startup hooks.

Source

pub fn pending_shutdown_hooks(&self) -> usize

Returns the number of pending shutdown hooks.

Trait Implementations§

Source§

impl Debug for App

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Handler for App

Source§

fn call<'a>( &'a self, ctx: &'a RequestContext, req: &'a mut Request, ) -> BoxFuture<'a, Response>

Process a request and return a response.
Source§

fn dependency_overrides(&self) -> Option<Arc<DependencyOverrides>>

Optional dependency overrides to apply when building request contexts. Read more

Auto Trait Implementations§

§

impl !Freeze for App

§

impl !RefUnwindSafe for App

§

impl Send for App

§

impl Sync for App

§

impl Unpin for App

§

impl !UnwindSafe 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: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
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<T> Same for T

Source§

type Output = T

Should always be Self
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
Source§

impl<T> ResponseProduces<T> for T