pub struct Router { /* private fields */ }Expand description
HTTP Router with Laravel-like route registration
Implementations§
Source§impl Router
impl Router
Sourcepub fn group<F>(self, prefix: &str, builder_fn: F) -> GroupBuilder
pub fn group<F>(self, prefix: &str, builder_fn: F) -> GroupBuilder
Create a route group with a shared prefix
Routes defined within the group will have the prefix prepended to their paths. Middleware applied to the group will be applied to all routes within it.
§Example
Router::new()
.group("/api", |r| {
r.get("/users", list_users) // -> GET /api/users
.post("/users", create_user) // -> POST /api/users
.get("/users/{id}", show_user) // -> GET /api/users/{id}
})
.middleware(ApiMiddleware)Source§impl Router
impl Router
Sourcepub fn get_route_middleware(&self, path: &str) -> Vec<BoxedMiddleware> ⓘ
pub fn get_route_middleware(&self, path: &str) -> Vec<BoxedMiddleware> ⓘ
Get middleware for a specific route path
Sourcepub fn get_fallback(&self) -> Option<(Arc<BoxedHandler>, Vec<BoxedMiddleware>)>
pub fn get_fallback(&self) -> Option<(Arc<BoxedHandler>, Vec<BoxedMiddleware>)>
Get the fallback handler and its middleware
Sourcepub fn get<H, Fut>(self, path: &str, handler: H) -> RouteBuilder
pub fn get<H, Fut>(self, path: &str, handler: H) -> RouteBuilder
Register a GET route
Sourcepub fn post<H, Fut>(self, path: &str, handler: H) -> RouteBuilder
pub fn post<H, Fut>(self, path: &str, handler: H) -> RouteBuilder
Register a POST route
Sourcepub fn put<H, Fut>(self, path: &str, handler: H) -> RouteBuilder
pub fn put<H, Fut>(self, path: &str, handler: H) -> RouteBuilder
Register a PUT route
Sourcepub fn patch<H, Fut>(self, path: &str, handler: H) -> RouteBuilder
pub fn patch<H, Fut>(self, path: &str, handler: H) -> RouteBuilder
Register a PATCH route
Sourcepub fn delete<H, Fut>(self, path: &str, handler: H) -> RouteBuilder
pub fn delete<H, Fut>(self, path: &str, handler: H) -> RouteBuilder
Register a DELETE route
Sourcepub fn match_route(
&self,
method: &Method,
path: &str,
) -> Option<(Arc<BoxedHandler>, HashMap<String, String>, String)>
pub fn match_route( &self, method: &Method, path: &str, ) -> Option<(Arc<BoxedHandler>, HashMap<String, String>, String)>
Match a request and return the handler with extracted params and route pattern
Returns (handler, params, route_pattern) where route_pattern is the original pattern like “/users/{id}” for metrics grouping.
OPTIONS requests are dispatched through match_preflight:
any path registered under any other verb returns a synthetic 204 handler so
route-level middleware (CORS in particular) still runs. The CORS middleware
then short-circuits the preflight with the configured ACAO / ACAH / ACAM
headers. Without this, OPTIONS would 404 before the middleware chain ran.
Trait Implementations§
Source§impl From<GroupBuilder> for Router
impl From<GroupBuilder> for Router
Source§fn from(builder: GroupBuilder) -> Self
fn from(builder: GroupBuilder) -> Self
Source§impl From<RouteBuilder> for Router
impl From<RouteBuilder> for Router
Source§fn from(builder: RouteBuilder) -> Self
fn from(builder: RouteBuilder) -> Self
Auto Trait Implementations§
impl !Freeze for Router
impl !RefUnwindSafe for Router
impl Send for Router
impl Sync for Router
impl Unpin for Router
impl UnsafeUnpin for Router
impl !UnwindSafe for Router
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more