pub struct App<S, T> { /* private fields */ }
Expand description
The Application of roa.
§Example
use roa_core::{App, Context, Next, Result, MiddlewareExt};
use tracing::info;
use tokio::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 tracing::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(())
}
Implementations§
Source§impl<S, T> App<S, T>where
T: for<'a> Middleware<'a, S>,
impl<S, T> App<S, T>where
T: for<'a> Middleware<'a, S>,
Trait Implementations§
Source§impl<S, E, IO> Service<&AddrStream<IO>> for App<S, Arc<E>>
impl<S, E, IO> Service<&AddrStream<IO>> for App<S, Arc<E>>
Auto Trait Implementations§
impl<S, T> Freeze for App<S, T>
impl<S, T> !RefUnwindSafe for App<S, T>
impl<S, T> Send for App<S, T>
impl<S, T> Sync for App<S, T>
impl<S, T> Unpin for App<S, T>
impl<S, T> !UnwindSafe for App<S, T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more