pub trait Endpoint<State: Clone + Send + Sync + 'static>:
Send
+ Sync
+ 'static {
// Required method
fn call<'life0, 'async_trait>(
&'life0 self,
req: Request<State>,
) -> Pin<Box<dyn Future<Output = Result> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
}
Expand description
An HTTP request handler.
This trait is automatically implemented for Fn
types, and so is rarely implemented
directly by Tide users.
In practice, endpoints are functions that take a Request<State>
as an argument and
return a type T
that implements Into<Response>
.
§Examples
Endpoints are implemented as asynchronous functions that make use of language features currently only available in Rust Nightly. For this reason, we have to explicitly enable the attribute will be omitted in most of the documentation.
A simple endpoint that is invoked on a GET
request and returns a String
:
async fn hello(_req: tide::Request<()>) -> tide::Result<String> {
Ok(String::from("hello"))
}
let mut app = tide::Server::new();
app.at("/hello").get(hello);
An endpoint with similar functionality that does not make use of the async
keyword would look something like this:
fn hello(_req: tide::Request<()>) -> impl Future<Output = tide::Result<String>> {
async_std::future::ready(Ok(String::from("hello")))
}
let mut app = tide::Server::new();
app.at("/hello").get(hello);
Tide routes will also accept endpoints with Fn
signatures of this form, but using the async
keyword has better ergonomics.