Skip to main content

Router

Struct Router 

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

Express-style router. Return from modules and app.mount("/blog", routes()).

Implementations§

Source§

impl Router

Source

pub fn new() -> Self

Source

pub fn use_middleware<M>(&mut self, mw: M) -> &mut Self
where M: IntoMwEntry,

Source

pub fn with<T: RouteValue>(&mut self, value: T) -> &mut Self

Attach a RouteValue to the last HTTP route, or to router defaults.

After get/post/…, writes to that route. Otherwise writes to router/app defaults (inherited by routes: route > router > app).

Source

pub fn with_update<T, F>(&mut self, f: F) -> &mut Self
where T: RouteValue + Clone + Default, F: FnOnce(&mut T),

Update a RouteValue on the last route (insert T::default() if missing).

Source

pub fn route_middleware<M>(&mut self, mw: M) -> &mut Self
where M: IntoMwEntry,

Push middleware onto the last registered HTTP route only.

Source

pub fn route_meta<T: RouteValue>(&mut self, value: T) -> &mut Self

Alias for Self::with (writes to the last route when present).

Source

pub fn state<T>(&mut self, value: T) -> &mut Self
where T: Send + Sync + 'static,

Source

pub fn try_state<T>(&self) -> Option<Arc<T>>
where T: Send + Sync + 'static,

Shared app state inserted via Self::state, if present.

Source

pub fn get<H, T>(&mut self, path: &str, handler: H) -> &mut Self
where H: IntoHandler<T>,

Source

pub fn post<H, T>(&mut self, path: &str, handler: H) -> &mut Self
where H: IntoHandler<T>,

Source

pub fn put<H, T>(&mut self, path: &str, handler: H) -> &mut Self
where H: IntoHandler<T>,

Source

pub fn patch<H, T>(&mut self, path: &str, handler: H) -> &mut Self
where H: IntoHandler<T>,

Source

pub fn delete<H, T>(&mut self, path: &str, handler: H) -> &mut Self
where H: IntoHandler<T>,

Source

pub fn redirect( &mut self, from: &str, to: impl Into<String>, status: u16, ) -> &mut Self

GET from → redirect to to with the given HTTP status (e.g. 302, 301, 303).

app.redirect("/health", "/healthz", 302);
app.redirect("/old", "/new", 301);
Source

pub fn raw<H>(&mut self, path: &str, handler: H) -> &mut Self
where H: IntoRawHandler,

Escape hatch: handle a path with a raw Hyper request/response (no Sova middleware).

Source

pub fn mount(&mut self, prefix: &str, other: Router) -> &mut Self

Mount a child router under prefix.

Bakes the child’s middleware stack into its routes. Does not prepend this router’s middleware — the eventual root stack is applied once in compile_router as an outer wrap (so App-level middleware is not doubled).

Source

pub fn group<F>(&mut self, prefix: &str, f: F) -> &mut Self
where F: FnOnce(&mut Router),

Sugar over Self::mount: build a child router in a closure.

Source

pub fn catch<H, T>(&mut self, status: u16, handler: H) -> &mut Self
where H: IntoHandler<T>,

Register a catcher for HTTP status in this router’s mount scope.

At dispatch, the catcher with the longest matching prefix wins. not_found is sugar for catch(404, …).

Source

pub fn not_found<H, T>(&mut self, handler: H) -> &mut Self
where H: IntoHandler<T>,

Sugar for Self::catch(404, handler).

Source

pub fn error_handler<F, Fut>(&mut self, f: F) -> &mut Self
where F: Fn(Error) -> Fut + Send + Sync + 'static, Fut: Future<Output = Response> + Send + 'static,

Called when a leaf handler returns Err. Request is already consumed.

Source

pub fn route_entries(&self) -> Vec<RouteEntry>

Full introspection: HTTP routes and raw paths.

Source

pub fn explain(&self) -> String

Human-readable route map (method, path, middleware names).

Trait Implementations§

Source§

impl Default for Router

Source§

fn default() -> Self

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

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more