Skip to main content

Router

Struct Router 

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

Collects routes, then answers requests.

Implementations§

Source§

impl Router

Source

pub fn new() -> Self

Source

pub fn middleware(&mut self, middleware: impl Middleware) -> &mut Self

Add middleware. Inside a group it applies to that group’s routes; at the top level it applies to every request, including 404s.

Source

pub fn group( &mut self, prefix: &str, define: impl FnOnce(&mut Router), ) -> &mut Self

Register routes under a shared prefix and middleware stack.

Source

pub fn version( &mut self, version: &str, define: impl FnOnce(&mut Router), ) -> &mut Self

Register one version of an API: a group under /{version} whose routes know which version they are.

r.version("v1", |v1| { v1.get("/users", v1::users::index); });
r.version("v2", |v2| { v2.get("/users", v2::users::index); });

A handler can read the version with req.api_version(), which lets one handler serve two versions where the difference is small, and generated documentation groups routes by it. Versioning by header instead of path is crate::versioning::VersionHeader.

Source

pub fn fallback(&mut self, handler: impl Handler) -> &mut Self

The response when nothing matched. Defaults to a plain 404.

Source

pub fn route( &mut self, method: Method, pattern: &str, handler: impl Handler, ) -> RouteHandle<'_>

Source

pub fn get(&mut self, pattern: &str, handler: impl Handler) -> RouteHandle<'_>

Source

pub fn post(&mut self, pattern: &str, handler: impl Handler) -> RouteHandle<'_>

Source

pub fn put(&mut self, pattern: &str, handler: impl Handler) -> RouteHandle<'_>

Source

pub fn patch(&mut self, pattern: &str, handler: impl Handler) -> RouteHandle<'_>

Source

pub fn delete( &mut self, pattern: &str, handler: impl Handler, ) -> RouteHandle<'_>

Source

pub fn resource<'r>(&'r mut self, base: &str) -> Resource<'r>

Start a RESTful resource: r.resource("/posts").index(..).show(..).

Source

pub fn finalize(&mut self)

Sort routes so lookups are deterministic. Called once before serving.

Source

pub fn routes(&self) -> &[Route]

Source

pub fn url_for(&self, name: &str, params: &[(&str, &str)]) -> Option<String>

Build a URL from a named route: url_for("users.show", &[("id", "7")]).

Source

pub async fn dispatch(&self, request: Request) -> Response

Match a request and run it through the pipeline.

Trait Implementations§

Source§

impl Default for Router

Source§

fn default() -> Router

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

Auto Trait Implementations§

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.