[][src]Struct hreq::server::Router

pub struct Router<State> { /* fields omitted */ }

Encapsulate chains of Middleware and Handler.

Inside Server there is always a default router which is configured via Server::at, however routers can also be instantiated and configured separately. This can be a good strategy for complex servers with many subsystems.

Example

use hreq::prelude::*;

async fn start_server() {
   let mut server = Server::new();

   let mut router = Router::new();
   router.at("/hello/:name").get(hello_there);

   // Resulting route is /much/hello/<name>
   server.at("/much").router(router);

   server.listen(3000).await.unwrap();
}

async fn hello_there(req: http::Request<Body>) -> String {
   format!("Hello {}", req.path_param("name").unwrap())
}

Implementations

impl<State> Router<State> where
    State: Clone + Unpin + Send + Sync + 'static, 
[src]

pub fn new() -> Router<State>[src]

Creates a new router.

pub fn at(&mut self, path: &str) -> Route<'_, State>[src]

Configure an route for this server.

A route is a chain of zero or more Middleware followed by a Handler.

Reusing the same path will overwrite the previous config.

Trait Implementations

impl<State: Clone> Clone for Router<State>[src]

impl<State> Debug for Router<State>[src]

Auto Trait Implementations

impl<State> !RefUnwindSafe for Router<State>

impl<State> Send for Router<State>

impl<State> Sync for Router<State>

impl<State> Unpin for Router<State>

impl<State> !UnwindSafe for Router<State>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Sealed<T> for T where
    T: ?Sized

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> WithSubscriber for T[src]