tide

Trait Endpoint

Source
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.

Required Methods§

Source

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,

Invoke the endpoint within the given context

Trait Implementations§

Source§

impl<State: Clone + Send + Sync + 'static> Endpoint<State> for Box<dyn Endpoint<State>>

Source§

fn call<'life0, 'async_trait>( &'life0 self, request: Request<State>, ) -> Pin<Box<dyn Future<Output = Result> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Invoke the endpoint within the given context

Implementations on Foreign Types§

Source§

impl<State: Clone + Send + Sync + 'static> Endpoint<State> for Box<dyn Endpoint<State>>

Source§

fn call<'life0, 'async_trait>( &'life0 self, request: Request<State>, ) -> Pin<Box<dyn Future<Output = Result> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Implementors§

Source§

impl<F, Fut, State> Endpoint<State> for SseEndpoint<F, Fut, State>
where State: Clone + Send + Sync + 'static, F: Fn(Request<State>, Sender) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<()>> + Send + 'static,

Source§

impl<State, F, Fut, Res> Endpoint<State> for F
where State: Clone + Send + Sync + 'static, F: Send + Sync + 'static + Fn(Request<State>) -> Fut, Fut: Future<Output = Result<Res>> + Send + 'static, Res: Into<Response> + 'static,

Source§

impl<State, T> Endpoint<State> for Redirect<T>
where State: Clone + Send + Sync + 'static, T: AsRef<str> + Send + Sync + 'static,

Source§

impl<State: Clone + Sync + Send + 'static, InnerState: Clone + Sync + Send + 'static> Endpoint<State> for Server<InnerState>