[][src]Struct roa::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) -> App<S, ()>[src]

This is supported on feature="runtime" only.

Construct app with default runtime.

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

pub fn new() -> App<(), ()>[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 Spawn + Send + Sync + 'static
) -> App<S, ()>
[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, App<S, Arc<E>>, Executor> where
    I: Accept<Conn = AddrStream<IO>>,
    IO: 'static + Send + Sync + Unpin + AsyncRead + AsyncWrite,
    S: State,
    <I as Accept>::Error: Into<Box<dyn Error + 'static + Send + Sync>>, 
[src]

Construct a hyper server by an incoming.

Trait Implementations

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

type Server = Server<TcpIncoming, Self, Executor>

This is supported on feature="tcp" only.

http server

impl<'_, S, E, IO> Service<&'_ AddrStream<IO>> for App<S, Arc<E>> where
    E: for<'a> Endpoint<'a, S>,
    IO: 'static + Send + Sync + Unpin + AsyncRead + AsyncWrite,
    S: State
[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>, Error>> + 'static + Send>>

The future response value.

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

type Server = Server<TlsIncoming<TcpIncoming>, Self, Executor>

This is supported on feature="tls" and feature="tcp" only.

http server

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> Same<T> for T

type Output = T

Should always be Self

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.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,