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);