Struct roa_core::App [−][src]
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(()) }
Implementations
impl<S> App<S, ()>[src]
pub fn state(state: S) -> Self[src]
This is supported on crate feature
runtime only.Construct app with default runtime.
impl App<(), ()>[src]
pub fn new() -> Self[src]
This is supported on crate 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]
T: for<'a> Middleware<'a, S>,
pub fn gate<M>(self, middleware: M) -> App<S, Chain<T, M>> where
M: for<'a> Middleware<'a, S>, [src]
M: for<'a> Middleware<'a, S>,
Use a middleware.
pub fn end<E>(self, endpoint: E) -> App<S, Arc<Chain<T, E>>> where
E: for<'a> Endpoint<'a, S>, [src]
E: for<'a> Endpoint<'a, S>,
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]
E: for<'a> Endpoint<'a, S>,
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
S: MakeServiceRef<IO, Body, ResBody = B>,
E: H2Exec<<<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>,
I: Accept<Conn = IO, Error = IE>,
IO: AsyncRead + AsyncWrite + Unpin + Send + 'static,
B: Body + 'static,
IE: Into<Box<dyn Error + 'static + Sync + Send, Global>>,
<S as MakeServiceRef<IO, Body>>::Error: Into<Box<dyn Error + 'static + Sync + Send, Global>>,
<B as Body>::Error: Into<Box<dyn Error + 'static + Sync + Send, 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>>, [src]
Notable traits for Server<I, S, E>
impl<I, IO, IE, S, B, E> Future for Server<I, S, E> where
S: MakeServiceRef<IO, Body, ResBody = B>,
E: H2Exec<<<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>,
I: Accept<Conn = IO, Error = IE>,
IO: AsyncRead + AsyncWrite + Unpin + Send + 'static,
B: Body + 'static,
IE: Into<Box<dyn Error + 'static + Sync + Send, Global>>,
<S as MakeServiceRef<IO, Body>>::Error: Into<Box<dyn Error + 'static + Sync + Send, Global>>,
<B as Body>::Error: Into<Box<dyn Error + 'static + Sync + Send, Global>>, type Output = Result<(), Error>;S: State,
IO: 'static + Send + Sync + Unpin + AsyncRead + AsyncWrite,
I: Accept<Conn = AddrStream<IO>>,
I::Error: Into<Box<dyn Error + Send + Sync>>,
Construct a hyper server by an incoming.
Trait Implementations
impl Default for App<(), ()>[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]
S: State,
E: for<'a> Endpoint<'a, S>,
IO: 'static + Send + Sync + Unpin + AsyncRead + AsyncWrite,
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.
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>[src]
fn call(&mut self, stream: &AddrStream<IO>) -> Self::Future[src]
Auto Trait Implementations
impl<S, T> !RefUnwindSafe for App<S, T>
impl<S, T> Send for App<S, T> where
S: Send,
T: Send,
S: Send,
T: Send,
impl<S, T> Sync for App<S, T> where
S: Sync,
T: Sync,
S: Sync,
T: Sync,
impl<S, T> Unpin for App<S, T> where
S: Unpin,
T: Unpin,
S: Unpin,
T: Unpin,
impl<S, T> !UnwindSafe for App<S, T>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized, [src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized, [src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized, [src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T[src]
impl<T> From<T> for T[src]
impl<T> Instrument for T[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>[src]
pub fn in_current_span(self) -> Instrumented<Self>[src]
impl<T> Instrument for T[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>[src]
pub fn in_current_span(self) -> Instrumented<Self>[src]
impl<T, U> Into<U> for T where
U: From<T>, [src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>, [src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>, [src]
U: TryFrom<T>,