[][src]Struct roa_core::App

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

The Application of roa.

Example

use roa_core::App;
use log::info;
use async_std::fs::File;

let mut app = App::new(());
app.gate_fn(|ctx, next| async move {
    info!("{} {}", ctx.method(), ctx.uri());
    next.await
});
app.end(|mut ctx| async move {
    ctx.resp_mut().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;
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 mut app = App::new(State::new());
app.gate_fn(|mut ctx, next| async move {
    ctx.id = 1;
    next.await
});
app.end(|ctx| async move {
    let id = ctx.id;
    ctx.database.lock().await.get(&id);
    Ok(())
});

Methods

impl<S: State> App<S>[src]

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

Construct app with default runtime.

impl<S: State> App<S>[src]

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

Construct an application with custom runtime.

pub fn gate(&mut self, middleware: impl Middleware<S>) -> &mut Self[src]

Use a middleware.

pub fn gate_fn<F>(
    &mut self,
    middleware: impl 'static + Sync + Send + Fn(Context<S>, Next) -> F
) -> &mut Self where
    F: 'static + Future<Output = Result>, 
[src]

A sugar to match a lambda as a middleware.

App::gate cannot match a lambda without parameter type indication.

use roa_core::{App, Next};

let mut app = App::new(());
// app.gate(|_ctx, next| async move { next.await }); compile fails.
app.gate(|_ctx, next: Next| async move { next.await });

However, with App::gate_fn, you can match a lambda without type indication.

use roa_core::{App, Next};

let mut app = App::new(());
app.gate_fn(|_ctx, next| async move { next.await });

pub fn end<F>(&mut self, endpoint: fn(_: Context<S>) -> F) -> &mut Self where
    F: 'static + Future<Output = Result>, 
[src]

A sugar to match a function pointer like async fn(Context<S>) -> impl Future and use it as a middleware(endpoint).

As the ducument of Middleware, an endpoint is defined as a template:

use roa_core::{App, Context, Result};
use std::future::Future;

fn endpoint<F>(ctx: Context<()>) -> F
where F: 'static + Send + Future<Output=Result> {
    unimplemented!()
}

However, an async function is not a template, it needs a transfer function to suit for App::gate.

use roa_core::{App, Context, Result, State, Middleware};
use std::future::Future;

async fn endpoint(ctx: Context<()>) -> Result {
    Ok(())
}

fn transfer<S, F>(endpoint: fn(Context<S>) -> F) -> impl Middleware<S>
where S: State,
      F: 'static + Future<Output=Result> {
    endpoint
}

App::new(()).gate(transfer(endpoint));

And App::end is a wrapper of App::gate with this transfer function.

use roa_core::App;
App::new(()).end(|_ctx| async { Ok(()) });

Important traits for Server<I, S, E>
pub fn accept<I>(&self, incoming: I) -> Server<I, Self, Executor> where
    I: Accept<Conn = AddrStream>,
    I::Error: Into<Box<dyn StdError + Send + Sync>>, 
[src]

Construct a hyper server by an incoming.

Trait Implementations

impl<S: State> Clone for App<S>[src]

impl<'_, S: State> Service<&'_ AddrStream> for App<S>[src]

type Response = HttpService<S>

Responses given by the service.

type Error = Error

Errors produced by the service.

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

The future response value.

Auto Trait Implementations

impl<S> !RefUnwindSafe for App<S>

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

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

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

impl<S> !UnwindSafe for App<S>

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.