pub trait Endpoint<'a, S = ()>: 'static + Sync + Send {
fn call<'async_trait>(
&'a self,
ctx: &'a mut Context<S>
) -> Pin<Box<dyn Future<Output = Result<(), Status>> + 'async_trait, Global>>
where
'a: 'async_trait,
Self: '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
Implementations on Foreign Types
String endpoint.
Static slice endpoint.
ok endpoint, always return Ok(())
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 = ()>,
This is supported on crate feature
websocket
only.Redirect endpoint.
impl<'a, S> Endpoint<'a, S> for Dispatcher<S> where
S: 'static,
This is supported on crate feature
router
only.impl<'a, S> Endpoint<'a, S> for RouteTable<S> where
S: 'static,
This is supported on crate feature
router
only.status endpoint.
impl<'a, S, E> Endpoint<'a, S> for Guard<E> where
E: Endpoint<'a, S>,
This is supported on crate feature
router
only.impl<'a, S, R> Endpoint<'a, S> for RpcEndpoint<R> where
S: State,
R: Router + Sync + Send + 'static,
This is supported on crate feature
jsonrpc
only.