Struct Router

Source
pub struct Router<State> { /* private fields */ }
Expand description

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§

Source§

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

Source

pub fn new() -> Router<State>

Creates a new router.

Source

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

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§

Source§

impl<State: Clone> Clone for Router<State>

Source§

fn clone(&self) -> Router<State>

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

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

Performs copy-assignment from source. Read more
Source§

impl<State> Debug for Router<State>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<State> Freeze for Router<State>

§

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§

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.