[−][src]Struct roa::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::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(()) }
Methods
impl<S> App<S, ()>
[src]
pub fn state(state: S) -> App<S, ()>
[src]
This is supported on
feature="runtime"
only.Construct app with default runtime.
impl App<(), ()>
[src]
pub fn new() -> App<(), ()>
[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 Spawn + Send + Sync + 'static
) -> App<S, ()>
[src]
state: S,
exec: impl Spawn + Send + Sync + 'static
) -> App<S, ()>
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, App<S, Arc<E>>, Executor> where
I: Accept<Conn = AddrStream<IO>>,
IO: 'static + Send + Sync + Unpin + AsyncRead + AsyncWrite,
S: State,
<I as Accept>::Error: Into<Box<dyn Error + 'static + Send + Sync>>,
[src]
I: Accept<Conn = AddrStream<IO>>,
IO: 'static + Send + Sync + Unpin + AsyncRead + AsyncWrite,
S: State,
<I as Accept>::Error: Into<Box<dyn Error + 'static + Send + Sync>>,
Construct a hyper server by an incoming.
Trait Implementations
impl<S, E> Listener for App<S, Arc<E>> where
S: State,
E: for<'a> Endpoint<'a, S>,
[src]
S: State,
E: for<'a> Endpoint<'a, S>,
type Server = Server<TcpIncoming, Self, Executor>
This is supported on
feature="tcp"
only.http server
fn bind(self, addr: impl ToSocketAddrs) -> Result<(SocketAddr, Self::Server)>
[src]
fn listen(
self,
addr: impl ToSocketAddrs,
callback: impl Fn(SocketAddr)
) -> Result<Self::Server>
[src]
self,
addr: impl ToSocketAddrs,
callback: impl Fn(SocketAddr)
) -> Result<Self::Server>
fn run(self) -> Result<(SocketAddr, Self::Server)>
[src]
impl<'_, S, E, IO> Service<&'_ AddrStream<IO>> for App<S, Arc<E>> where
E: for<'a> Endpoint<'a, S>,
IO: 'static + Send + Sync + Unpin + AsyncRead + AsyncWrite,
S: State,
[src]
E: for<'a> Endpoint<'a, S>,
IO: 'static + Send + Sync + Unpin + AsyncRead + AsyncWrite,
S: State,
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>, Error>> + 'static + Send>>
The future response value.
fn poll_ready(
&mut self,
_cx: &mut Context
) -> Poll<Result<(), <App<S, Arc<E>> as Service<&'_ AddrStream<IO>>>::Error>>
[src]
&mut self,
_cx: &mut Context
) -> Poll<Result<(), <App<S, Arc<E>> as Service<&'_ AddrStream<IO>>>::Error>>
fn call(
&mut self,
stream: &AddrStream<IO>
) -> <App<S, Arc<E>> as Service<&'_ AddrStream<IO>>>::Future
[src]
&mut self,
stream: &AddrStream<IO>
) -> <App<S, Arc<E>> as Service<&'_ AddrStream<IO>>>::Future
impl<S, E> TlsListener for App<S, Arc<E>> where
S: State,
E: for<'a> Endpoint<'a, S>,
[src]
S: State,
E: for<'a> Endpoint<'a, S>,
type Server = Server<TlsIncoming<TcpIncoming>, Self, Executor>
This is supported on
feature="tls"
and feature="tcp"
only.http server
fn bind_tls(
self,
addr: impl ToSocketAddrs,
config: ServerConfig
) -> Result<(SocketAddr, Self::Server)>
[src]
self,
addr: impl ToSocketAddrs,
config: ServerConfig
) -> Result<(SocketAddr, Self::Server)>
fn listen_tls(
self,
addr: impl ToSocketAddrs,
config: ServerConfig,
callback: impl Fn(SocketAddr)
) -> Result<Self::Server>
[src]
self,
addr: impl ToSocketAddrs,
config: ServerConfig,
callback: impl Fn(SocketAddr)
) -> Result<Self::Server>
fn run_tls(self, config: ServerConfig) -> Result<(SocketAddr, Self::Server)>
[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> Same<T> for T
type Output = T
Should always be Self
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>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,