pub struct Router { /* private fields */ }Expand description
Express-style router. Return from modules and app.mount("/blog", routes()).
Implementations§
Source§impl Router
impl Router
pub fn new() -> Self
pub fn use_middleware<M>(&mut self, mw: M) -> &mut Selfwhere
M: IntoMwEntry,
Sourcepub fn with<T: RouteValue>(&mut self, value: T) -> &mut Self
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).
Sourcepub fn with_update<T, F>(&mut self, f: F) -> &mut Self
pub fn with_update<T, F>(&mut self, f: F) -> &mut Self
Update a RouteValue on the last route (insert T::default() if missing).
Sourcepub fn route_middleware<M>(&mut self, mw: M) -> &mut Selfwhere
M: IntoMwEntry,
pub fn route_middleware<M>(&mut self, mw: M) -> &mut Selfwhere
M: IntoMwEntry,
Push middleware onto the last registered HTTP route only.
Sourcepub fn route_meta<T: RouteValue>(&mut self, value: T) -> &mut Self
pub fn route_meta<T: RouteValue>(&mut self, value: T) -> &mut Self
Alias for Self::with (writes to the last route when present).
pub fn state<T>(&mut self, value: T) -> &mut Self
Sourcepub fn try_state<T>(&self) -> Option<Arc<T>>
pub fn try_state<T>(&self) -> Option<Arc<T>>
Shared app state inserted via Self::state, if present.
pub fn get<H, T>(&mut self, path: &str, handler: H) -> &mut Selfwhere
H: IntoHandler<T>,
pub fn post<H, T>(&mut self, path: &str, handler: H) -> &mut Selfwhere
H: IntoHandler<T>,
pub fn put<H, T>(&mut self, path: &str, handler: H) -> &mut Selfwhere
H: IntoHandler<T>,
pub fn patch<H, T>(&mut self, path: &str, handler: H) -> &mut Selfwhere
H: IntoHandler<T>,
pub fn delete<H, T>(&mut self, path: &str, handler: H) -> &mut Selfwhere
H: IntoHandler<T>,
Sourcepub fn head<H, T>(&mut self, path: &str, handler: H) -> &mut Selfwhere
H: IntoHandler<T>,
pub fn head<H, T>(&mut self, path: &str, handler: H) -> &mut Selfwhere
H: IntoHandler<T>,
Explicit HEAD handler (takes precedence over GET→strip-body).
Sourcepub fn options<H, T>(&mut self, path: &str, handler: H) -> &mut Selfwhere
H: IntoHandler<T>,
pub fn options<H, T>(&mut self, path: &str, handler: H) -> &mut Selfwhere
H: IntoHandler<T>,
Explicit OPTIONS handler (takes precedence over auto 204 + Allow).
Sourcepub fn redirect(
&mut self,
from: &str,
to: impl Into<String>,
status: u16,
) -> &mut Self
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);Sourcepub fn raw<H>(&mut self, path: &str, handler: H) -> &mut Selfwhere
H: IntoRawHandler,
pub fn raw<H>(&mut self, path: &str, handler: H) -> &mut Selfwhere
H: IntoRawHandler,
Escape hatch: handle a path with a raw Hyper request/response (no Sova middleware).
Sourcepub fn mount(&mut self, prefix: &str, other: Router) -> &mut Self
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).
Sourcepub fn group<F>(&mut self, prefix: &str, f: F) -> &mut Self
pub fn group<F>(&mut self, prefix: &str, f: F) -> &mut Self
Sugar over Self::mount: build a child router in a closure.
Sourcepub fn catch<H, T>(&mut self, status: u16, handler: H) -> &mut Selfwhere
H: IntoHandler<T>,
pub fn catch<H, T>(&mut self, status: u16, handler: H) -> &mut Selfwhere
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, …).
Sourcepub fn not_found<H, T>(&mut self, handler: H) -> &mut Selfwhere
H: IntoHandler<T>,
pub fn not_found<H, T>(&mut self, handler: H) -> &mut Selfwhere
H: IntoHandler<T>,
Sugar for Self::catch(404, handler).
Sourcepub fn error_handler<F, Fut>(&mut self, f: F) -> &mut Self
pub fn error_handler<F, Fut>(&mut self, f: F) -> &mut Self
Called when a leaf handler returns Err. Request is already consumed.
Sourcepub fn route_entries(&self) -> Vec<RouteEntry>
pub fn route_entries(&self) -> Vec<RouteEntry>
Full introspection: HTTP routes and raw paths.