Struct Router

Source
pub struct Router<Body, Data: Clone, Error> { /* private fields */ }
Expand description

A router that can be used as a Service.

§Example

use std::convert::Infallible;

use http::{Request, Response, StatusCode};
use tower::Service;
use router_service::Router;

let mut router = Router::new()
    .get("/", |_, _| async move {
        Response::builder().body(())
    });

let req = Request::get("/").body(()).unwrap();
let resp = router.call(req).await.unwrap();
assert_eq!(resp.status(), 200);

Implementations§

Source§

impl<Body, Error> Router<Body, (), Error>

Source

pub fn new() -> Self

Create a new router that doesn’t require any data to be passed to handlers.

Source§

impl<Body, Data, Error> Router<Body, Data, Error>
where Body: 'static, Data: Clone + 'static, Error: 'static,

Source

pub fn with_data(data: Data) -> Self

Create a new router that requires data to be passed to handlers.

§Example
use std::convert::Infallible;

use http::{Request, Response, StatusCode};
use tower::Service;
use router_service::Router;

let mut router = Router::with_data(42)
    .get("/", |_, data| async move {
        println!("Got data! {}", data.data);
        Response::builder().body(())
    });

let req = Request::get("/").body(()).unwrap();
let resp = router.call(req).await.unwrap();
assert_eq!(resp.status(), 200);
Source

pub fn get<HandlerFn, Fut>( self, path: impl AsRef<str>, handler: HandlerFn, ) -> Self
where HandlerFn: Fn(Request<Body>, RouteContext<Data>) -> Fut + Sync + Send + 'static, Fut: Future<Output = Result<Response<Body>, Error>> + Send + Sync + 'static,

Registers a route requiring the GET method.

Source

pub fn post<HandlerFn, Fut>( self, path: impl AsRef<str>, handler: HandlerFn, ) -> Self
where HandlerFn: Fn(Request<Body>, RouteContext<Data>) -> Fut + Sync + Send + 'static, Fut: Future<Output = Result<Response<Body>, Error>> + Send + Sync + 'static,

Registers a route requiring the POST method.

Source

pub fn put<HandlerFn, Fut>( self, path: impl AsRef<str>, handler: HandlerFn, ) -> Self
where HandlerFn: Fn(Request<Body>, RouteContext<Data>) -> Fut + Sync + Send + 'static, Fut: Future<Output = Result<Response<Body>, Error>> + Send + Sync + 'static,

Registers a route requiring the PUT method.

Source

pub fn delete<HandlerFn, Fut>( self, path: impl AsRef<str>, handler: HandlerFn, ) -> Self
where HandlerFn: Fn(Request<Body>, RouteContext<Data>) -> Fut + Sync + Send + 'static, Fut: Future<Output = Result<Response<Body>, Error>> + Send + Sync + 'static,

Registers a route requiring the DELETE method.

Source

pub fn head<HandlerFn, Fut>( self, path: impl AsRef<str>, handler: HandlerFn, ) -> Self
where HandlerFn: Fn(Request<Body>, RouteContext<Data>) -> Fut + Sync + Send + 'static, Fut: Future<Output = Result<Response<Body>, Error>> + Send + Sync + 'static,

Registers a route requiring the HEAD method.

Source

pub fn options<HandlerFn, Fut>( self, path: impl AsRef<str>, handler: HandlerFn, ) -> Self
where HandlerFn: Fn(Request<Body>, RouteContext<Data>) -> Fut + Sync + Send + 'static, Fut: Future<Output = Result<Response<Body>, Error>> + Send + Sync + 'static,

Registers a route requiring the OPTIONS method.

Source

pub fn patch<HandlerFn, Fut>( self, path: impl AsRef<str>, handler: HandlerFn, ) -> Self
where HandlerFn: Fn(Request<Body>, RouteContext<Data>) -> Fut + Sync + Send + 'static, Fut: Future<Output = Result<Response<Body>, Error>> + Send + Sync + 'static,

Registers a route requiring the PATCH method.

Source

pub fn any<HandlerFn, Fut>( self, path: impl AsRef<str>, handler: HandlerFn, ) -> Self
where HandlerFn: Fn(Request<Body>, RouteContext<Data>) -> Fut + Sync + Send + 'static, Fut: Future<Output = Result<Response<Body>, Error>> + Send + Sync + 'static,

Registers a route matching any method.

Trait Implementations§

Source§

impl<Body, Data, Error> Clone for Router<Body, Data, Error>
where Data: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<Body: Default, Data: Default + Clone, Error: Default> Default for Router<Body, Data, Error>

Source§

fn default() -> Router<Body, Data, Error>

Returns the “default value” for a type. Read more
Source§

impl<Body, Data, Error> Service<Request<Body>> for Router<Body, Data, Error>
where Body: Default, Data: Clone,

Source§

type Response = Response<Body>

Responses given by the service.
Source§

type Error = Error

Errors produced by the service.
Source§

type Future = ResponseFuture<Body, Error>

The future response value.
Source§

fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>>

Returns Poll::Ready(Ok(())) when the service is able to process requests. Read more
Source§

fn call(&mut self, req: Request<Body>) -> Self::Future

Process the request and return the response asynchronously. Read more

Auto Trait Implementations§

§

impl<Body, Data, Error> Freeze for Router<Body, Data, Error>
where Data: Freeze,

§

impl<Body, Data, Error> RefUnwindSafe for Router<Body, Data, Error>
where Data: RefUnwindSafe,

§

impl<Body, Data, Error> Send for Router<Body, Data, Error>
where Data: Send,

§

impl<Body, Data, Error> Sync for Router<Body, Data, Error>
where Data: Sync,

§

impl<Body, Data, Error> Unpin for Router<Body, Data, Error>
where Data: Unpin,

§

impl<Body, Data, Error> UnwindSafe for Router<Body, Data, Error>
where Data: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.