Skip to main content

App

Struct App 

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

An HTTP server application with typed state, routing, and TLS support.

App<S> serves requests by routing them to handlers based on method and path. All handlers share access to a single S value (the app state), cloned as an Arc per request for zero-allocation sharing.

§Example

use mini_serve::App;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let app = App::new(());
    app.bind("127.0.0.1:8080".parse()?).await?;
    Ok(())
}

§Features

  • Routing: Register handlers for (Method, Path) pairs with path parameters (/users/:id).
  • State sharing: All handlers receive Arc<S> to the app state.
  • Request extraction: Parse bodies, extract path/query params, and build responses.
  • CORS: Optional cross-origin request handling with preflight validation.
  • TLS: Serve over HTTPS when the tls feature is enabled.
  • Graceful shutdown: Drain in-flight requests before exiting.

Implementations§

Source§

impl<S: Send + Sync + 'static> App<S>

Source

pub fn new(state: S) -> Self

Create a new app with shared state.

The state is wrapped in an Arc and shared with every request handler as State::from_arc(). Route registration is done via RouteBuilder.

Source

pub fn state_arc(&self) -> Arc<S>

Get an Arc to the app state.

Useful for spawning background tasks or accessing state outside the request-response loop.

Source

pub async fn route(&self, req: Request<Incoming>) -> Response<ResponseBody>

Source

pub async fn bind_ephemeral(self) -> Result<u16, ServeError>

Bind to an ephemeral port and serve in the background.

Returns the assigned port number. The server runs in a spawned task and serves until the process exits. For graceful shutdown, use run(). Binds to 127.0.0.1 only—safe for development and testing.

Source

pub async fn run<F>( self, listener: TcpListener, shutdown: F, ) -> Result<(), ServeError>
where F: Future<Output = ()> + Send + 'static,

Serve listener until shutdown resolves, then drain in-flight connections and return. The production entry point for callers that want control over the shutdown trigger (tests, custom signals); see App::bind for the OS-signal convenience wrapper.

Source

pub async fn bind(self, addr: SocketAddr) -> Result<(), ServeError>

Bind addr and serve until SIGINT or SIGTERM, then drain in-flight connections and return. Unlike App::bind_ephemeral, addr is caller-chosen — e.g. 0.0.0.0:$PORT for a platform like fly.io that routes external traffic to the process directly.

Source§

impl App<()>

Source

pub fn stateless() -> Self

Auto Trait Implementations§

§

impl<S> !RefUnwindSafe for App<S>

§

impl<S> !UnwindSafe for App<S>

§

impl<S> Freeze for App<S>

§

impl<S> Send for App<S>
where S: Sync + Send,

§

impl<S> Sync for App<S>
where S: Sync + Send,

§

impl<S> Unpin for App<S>

§

impl<S> UnsafeUnpin for App<S>

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<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