[−][src]Struct roa_core::App
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::new(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 new(state: S) -> Self
[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 '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> where
S: State,
IO: 'static + Send + Sync + Unpin + AsyncRead + AsyncWrite,
I: Accept<Conn = AddrStream<IO>>,
I::Error: Into<Box<dyn Error + Send + Sync>>,
[src]
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<S: Clone> Clone for App<S, Arc<dyn Endpoint<'a, S>>>
[src]
fn clone(&self) -> Self
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[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,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
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.
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>,