[][src]Trait roa::Endpoint

pub trait Endpoint<'a, S = ()>: 'static + Send + Sync {
#[must_use]    fn call<'async_trait>(
        &'a self,
        ctx: &'a mut Context<S>
    ) -> Pin<Box<dyn Future<Output = Result<(), Status>> + 'async_trait>>
    where
        'a: 'async_trait,
        Self: 'async_trait
; }

Endpoint

An endpoint is a request handler.

Build-in endpoint

There are some build-in endpoints.

  • Functional endpoint

A normal functional endpoint is an async function with signature: async fn(&mut Context) -> Result.

use roa_core::{App, Context, Result};

async fn endpoint(ctx: &mut Context) -> Result {
    Ok(())
}

let app = App::new().end(endpoint);
  • Ok endpoint

() is an endpoint always return Ok(())

let app = roa_core::App::new().end(());
  • Status endpoint

Status is an endpoint always return Err(Status)

use roa_core::{App, status};
use roa_core::http::StatusCode;
let app = App::new().end(status!(StatusCode::BAD_REQUEST));
  • String endpoint

Write string to body.

use roa_core::App;

let app = App::new().end("Hello, world"); // static slice
let app = App::new().end("Hello, world".to_owned()); // string
  • Redirect endpoint

Redirect to an uri.

use roa_core::App;
use roa_core::http::Uri;

let app = App::new().end("/target".parse::<Uri>().unwrap());

Custom endpoint

You can implement custom Endpoint for your types.

use roa_core::{App, Endpoint, Context, Next, Result, async_trait};
use async_std::sync::Arc;
use std::time::Instant;

fn is_endpoint(endpoint: impl for<'a> Endpoint<'a>) {
}

struct Logger;

#[async_trait(?Send)]
impl <'a> Endpoint<'a> for Logger {
    async fn call(&'a self, ctx: &'a mut Context) -> Result {
        Ok(())
    }
}

let app = App::new().end(Logger);

Required methods

#[must_use]fn call<'async_trait>(
    &'a self,
    ctx: &'a mut Context<S>
) -> Pin<Box<dyn Future<Output = Result<(), Status>> + 'async_trait>> where
    'a: 'async_trait,
    Self: 'async_trait, 

Call this endpoint.

Loading content...

Implementations on Foreign Types

impl<'a, S> Endpoint<'a, S> for ()[src]

ok endpoint, always return Ok(())

impl<'a, S> Endpoint<'a, S> for String[src]

String endpoint.

impl<'a, S> Endpoint<'a, S> for &'static str[src]

Static slice endpoint.

Loading content...

Implementors

impl<'a, F, S, Fut> Endpoint<'a, S> for Websocket<F, S, Fut> where
    S: State,
    F: 'static + Sync + Send + Fn(Context<S>, SocketStream) -> Fut,
    Fut: 'static + Send + Future<Output = ()>, 
[src]

impl<'a, S> Endpoint<'a, S> for Uri[src]

Redirect endpoint.

impl<'a, S> Endpoint<'a, S> for Dispatcher<S> where
    S: 'static, 
[src]

impl<'a, S> Endpoint<'a, S> for RouteTable<S> where
    S: 'static, 
[src]

impl<'a, S> Endpoint<'a, S> for Boxed<S> where
    S: 'static, 
[src]

impl<'a, S> Endpoint<'a, S> for Status[src]

status endpoint.

impl<'a, S, E> Endpoint<'a, S> for Guard<E> where
    E: Endpoint<'a, S>, 
[src]

impl<'a, S, T, F> Endpoint<'a, S> for T where
    F: 'a + Future<Output = Result<(), Status>>,
    S: 'a,
    T: 'static + Send + Sync + Fn(&'a mut Context<S>) -> F, 
[src]

impl<'a, S, T, U> Endpoint<'a, S> for Chain<T, U> where
    T: for<'b> Middleware<'b, S>,
    U: Endpoint<'a, S>, 
[src]

Loading content...