[−][src]Struct roa_core::App
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]
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]
&mut self,
middleware: impl 'static + Sync + Send + Fn(Context<S>, Next) -> F
) -> &mut Self where
F: 'static + Future<Output = Result>,
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]
F: 'static + Future<Output = Result>,
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]
I: Accept<Conn = AddrStream>,
I::Error: Into<Box<dyn StdError + Send + Sync>>,
Construct a hyper server by an incoming.
Trait Implementations
impl<S: State> Clone for App<S>[src]
fn clone(&self) -> Self[src]
fn clone_from(&mut self, source: &Self)1.0.0[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.
fn poll_ready(&mut self, _cx: &mut Context) -> Poll<StdResult<(), Self::Error>>[src]
fn call(&mut self, stream: &AddrStream) -> Self::Future[src]
Auto Trait Implementations
impl<S> !RefUnwindSafe for App<S>
impl<S> Send for App<S> where
S: Send,
S: Send,
impl<S> Sync for App<S> where
S: Sync,
S: Sync,
impl<S> Unpin for App<S> where
S: Unpin,
S: Unpin,
impl<S> !UnwindSafe for App<S>
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>,