Endpoint

Trait Endpoint 

Source
pub trait Endpoint<'a, S = ()>:
    'static
    + Sync
    + Send {
    // Required method
    fn call<'async_trait>(
        &'a self,
        ctx: &'a mut Context<S>,
    ) -> Pin<Box<dyn Future<Output = Result> + 'async_trait>>
       where Self: 'async_trait,
             'a: 'async_trait;
}
Expand description

§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};

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

struct Service;

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

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

Required Methods§

Source

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

Call this endpoint.

Implementations on Foreign Types§

Source§

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

Static slice endpoint.

Source§

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

Source§

impl<'a, S> Endpoint<'a, S> for ()

ok endpoint, always return Ok(())

Source§

fn call<'async_trait>( &'a self, _ctx: &'a mut Context<S>, ) -> Pin<Box<dyn Future<Output = Result> + 'async_trait>>
where Self: 'async_trait, 'a: 'async_trait,

Source§

impl<'a, S> Endpoint<'a, S> for String

String endpoint.

Source§

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

Source§

impl<'a, S> Endpoint<'a, S> for Uri

Redirect endpoint.

Source§

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

Implementors§

Source§

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

Source§

impl<'a, S> Endpoint<'a, S> for Status

status endpoint.

Source§

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

Source§

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