[][src]Struct roa_core::App

pub struct App<S, T> { /* fields omitted */ }

The Application of roa.

Example

use roa_core::{App, Context, Next, Result, MiddlewareExt};
use log::info;
use async_std::fs::File;

let app = App::new().gate(gate).end(end);
async fn gate(ctx: &mut Context, next: Next<'_>) -> Result {
    info!("{} {}", ctx.method(), ctx.uri());
    next.await
}

async fn end(ctx: &mut Context) -> Result {
    ctx.resp.write_reader(File::open("assets/welcome.html").await?);
    Ok(())
}

State

The State is designed to share data or handler between middlewares. The only one type implemented State by this crate is (), you can implement your custom state if neccassary.

use roa_core::{App, Context, Next, Result};
use log::info;
use futures::lock::Mutex;
use std::sync::Arc;
use std::collections::HashMap;

#[derive(Clone)]
struct State {
    id: u64,
    database: Arc<Mutex<HashMap<u64, String>>>,
}

impl State {
    fn new() -> Self {
        Self {
            id: 0,
            database: Arc::new(Mutex::new(HashMap::new()))
        }
    }
}

let app = App::state(State::new()).gate(gate).end(end);
async fn gate(ctx: &mut Context<State>, next: Next<'_>) -> Result {
    ctx.id = 1;
    next.await
}

async fn end(ctx: &mut Context<State>) -> Result {
    let id = ctx.id;
    ctx.database.lock().await.get(&id);
    Ok(())
}

Methods

impl<S> App<S, ()>[src]

pub fn state(state: S) -> Self[src]

This is supported on feature="runtime" only.

Construct app with default runtime.

impl App<(), ()>[src]

pub fn new() -> Self[src]

This is supported on feature="runtime" only.

Construct app with default runtime.

impl<S> App<S, ()>[src]

pub fn with_exec(state: S, exec: impl 'static + Send + Sync + Spawn) -> Self[src]

Construct an application with custom runtime.

impl<S, T> App<S, T> where
    T: for<'a> Middleware<'a, S>, 
[src]

pub fn gate<M>(self, middleware: M) -> App<S, Chain<T, M>> where
    M: for<'a> Middleware<'a, S>, 
[src]

Use a middleware.

pub fn end<E>(self, endpoint: E) -> App<S, Arc<Chain<T, E>>> where
    E: for<'a> Endpoint<'a, S>, 
[src]

Set endpoint, then app can only be used to serve http request.

impl<S, E> App<S, Arc<E>> where
    E: for<'a> Endpoint<'a, S>, 
[src]

pub fn accept<I, IO>(self, incoming: I) -> Server<I, Self, Executor> where
    S: State,
    IO: 'static + Send + Sync + Unpin + AsyncRead + AsyncWrite,
    I: Accept<Conn = AddrStream<IO>>,
    I::Error: Into<Box<dyn Error + Send + Sync>>, 
[src]

Construct a hyper server by an incoming.

Trait Implementations

impl<S: Clone> Clone for App<S, Arc<dyn Endpoint<'a, S>>>[src]

impl<'_, S, E, IO> Service<&'_ AddrStream<IO>> for App<S, Arc<E>> where
    S: State,
    E: for<'a> Endpoint<'a, S>,
    IO: 'static + Send + Sync + Unpin + AsyncRead + AsyncWrite
[src]

type Response = HttpService<S, E>

Responses given by the service.

type Error = Error

Errors produced by the service.

type Future = Pin<Box<dyn Future<Output = Result<HttpService<S, E>>> + Send + 'static>>

The future response value.

Auto Trait Implementations

impl<S, T> !RefUnwindSafe for App<S, T>

impl<S, T> Send for App<S, T> where
    S: Send,
    T: Send

impl<S, T> Sync for App<S, T> where
    S: Sync,
    T: Sync

impl<S, T> Unpin for App<S, T> where
    S: Unpin,
    T: Unpin

impl<S, T> !UnwindSafe for App<S, T>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.