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
pub fn accept<I, IO>(self, incoming: I) -> Server<I, Self, Executor>ⓘNotable traits for Server<I, S, E>impl<I, IO, IE, S, B, E> Future for Server<I, S, E> where
I: Accept<Conn = IO, Error = IE>,
IE: Into<Box<dyn Error + Sync + Send + 'static, Global>>,
IO: 'static + AsyncRead + AsyncWrite + Unpin + Send,
S: MakeServiceRef<IO, Body, ResBody = B>,
B: 'static + Body,
E: ConnStreamExec<<<S as MakeServiceRef<IO, Body>>::Service as HttpService<Body>>::Future, B> + NewSvcExec<IO, <S as MakeServiceRef<IO, Body>>::Future, <S as MakeServiceRef<IO, Body>>::Service, E, NoopWatcher>,
<S as MakeServiceRef<IO, Body>>::Error: Into<Box<dyn Error + Sync + Send + 'static, Global>>,
<B as Body>::Error: Into<Box<dyn Error + Sync + Send + 'static, Global>>, type Output = Result<(), Error>;
where
S: State,
IO: 'static + Send + Sync + Unpin + AsyncRead + AsyncWrite,
I: Accept<Conn = AddrStream<IO>>,
I::Error: Into<Box<dyn Error + Send + Sync>>,
pub fn accept<I, IO>(self, incoming: I) -> Server<I, Self, Executor>ⓘNotable traits for Server<I, S, E>impl<I, IO, IE, S, B, E> Future for Server<I, S, E> where
I: Accept<Conn = IO, Error = IE>,
IE: Into<Box<dyn Error + Sync + Send + 'static, Global>>,
IO: 'static + AsyncRead + AsyncWrite + Unpin + Send,
S: MakeServiceRef<IO, Body, ResBody = B>,
B: 'static + Body,
E: ConnStreamExec<<<S as MakeServiceRef<IO, Body>>::Service as HttpService<Body>>::Future, B> + NewSvcExec<IO, <S as MakeServiceRef<IO, Body>>::Future, <S as MakeServiceRef<IO, Body>>::Service, E, NoopWatcher>,
<S as MakeServiceRef<IO, Body>>::Error: Into<Box<dyn Error + Sync + Send + 'static, Global>>,
<B as Body>::Error: Into<Box<dyn Error + Sync + Send + 'static, Global>>, type Output = Result<(), Error>;
where
S: State,
IO: 'static + Send + Sync + Unpin + AsyncRead + AsyncWrite,
I: Accept<Conn = AddrStream<IO>>,
I::Error: Into<Box<dyn Error + Send + Sync>>,
Notable traits for Server<I, S, E>
impl<I, IO, IE, S, B, E> Future for Server<I, S, E> where
I: Accept<Conn = IO, Error = IE>,
IE: Into<Box<dyn Error + Sync + Send + 'static, Global>>,
IO: 'static + AsyncRead + AsyncWrite + Unpin + Send,
S: MakeServiceRef<IO, Body, ResBody = B>,
B: 'static + Body,
E: ConnStreamExec<<<S as MakeServiceRef<IO, Body>>::Service as HttpService<Body>>::Future, B> + NewSvcExec<IO, <S as MakeServiceRef<IO, Body>>::Future, <S as MakeServiceRef<IO, Body>>::Service, E, NoopWatcher>,
<S as MakeServiceRef<IO, Body>>::Error: Into<Box<dyn Error + Sync + Send + 'static, Global>>,
<B as Body>::Error: Into<Box<dyn Error + Sync + Send + 'static, Global>>, type Output = Result<(), Error>;
Construct a hyper server by an incoming.
Trait Implementations
Auto Trait Implementations
impl<S, T> !RefUnwindSafe for App<S, T>
impl<S, T> !UnwindSafe for App<S, T>
Blanket Implementations
Mutably borrows from an owned value. Read more
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more