App

Struct App 

Source
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§

Source§

impl<S> App<S, ()>

Source

pub fn state(state: S) -> Self

Available on crate feature runtime only.

Construct app with default runtime.

Source§

impl App<(), ()>

Source

pub fn new() -> Self

Available on crate feature runtime only.

Construct app with default runtime.

Source§

impl<S> App<S, ()>

Source

pub fn with_exec(state: S, exec: impl 'static + Send + Sync + Spawn) -> Self

Construct an application with custom runtime.

Source§

impl<S, T> App<S, T>
where T: for<'a> Middleware<'a, S>,

Source

pub fn gate<M>(self, middleware: M) -> App<S, Chain<T, M>>
where M: for<'a> Middleware<'a, S>,

Use a middleware.

Source

pub fn end<E>(self, endpoint: E) -> App<S, Arc<Chain<T, E>>>
where E: for<'a> Endpoint<'a, S>,

Set endpoint, then app can only be used to serve http request.

Source§

impl<S, E> App<S, Arc<E>>
where E: for<'a> Endpoint<'a, S>,

Source

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>>,

Construct a hyper server by an incoming.

Trait Implementations§

Source§

impl Default for App<(), ()>

Source§

fn default() -> Self

Construct app with default runtime.

Source§

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,

Source§

type Response = HttpService<S, E>

Responses given by the service.
Source§

type Error = Error

Errors produced by the service.
Source§

type Future = Pin<Box<dyn Future<Output = Result<HttpService<S, E>, Error>> + Send>>

The future response value.
Source§

fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>

Returns Poll::Ready(Ok(())) when the service is able to process requests. Read more
Source§

fn call(&mut self, stream: &AddrStream<IO>) -> Self::Future

Process the request and return the response asynchronously. Read more

Auto Trait Implementations§

§

impl<S, T> Freeze for App<S, T>
where T: Freeze, S: Freeze,

§

impl<S, T> !RefUnwindSafe for App<S, T>

§

impl<S, T> Send for App<S, T>
where T: Send, S: Send,

§

impl<S, T> Sync for App<S, T>
where T: Sync, S: Sync,

§

impl<S, T> Unpin for App<S, T>
where T: Unpin, S: Unpin,

§

impl<S, T> !UnwindSafe for App<S, T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more