pub struct Router { /* private fields */ }
Expand description
Router struct is used for route request to different handlers.
You can wite routers in flat way, like this:
Example
Router::with_path("writers").get(list_writers).post(create_writer);
Router::with_path("writers/<id>").get(show_writer).patch(edit_writer).delete(delete_writer);
Router::with_path("writers/<id>/articles").get(list_writer_articles);
You can write router like a tree, this is also the recommended way:
Example
use salvo_core::prelude::*;
Router::with_path("writers")
.get(list_writers)
.post(create_writer)
.push(
Router::with_path("<id>")
.get(show_writer)
.patch(edit_writer)
.delete(delete_writer)
.push(Router::with_path("articles").get(list_writer_articles)),
);
This form of definition can make the definition of router clear and simple for complex projects.
Implementations§
source§impl Router
impl Router
sourcepub fn routers_mut(&mut self) -> &mut Vec<Router, Global> ⓘ
pub fn routers_mut(&mut self) -> &mut Vec<Router, Global> ⓘ
Get current router’s children mutable reference.
sourcepub fn hoops(&self) -> &Vec<Arc<dyn Handler + 'static>, Global> ⓘ
pub fn hoops(&self) -> &Vec<Arc<dyn Handler + 'static>, Global> ⓘ
Get current router’s middlewares reference.
sourcepub fn hoops_mut(&mut self) -> &mut Vec<Arc<dyn Handler + 'static>, Global> ⓘ
pub fn hoops_mut(&mut self) -> &mut Vec<Arc<dyn Handler + 'static>, Global> ⓘ
Get current router’s middlewares mutable reference.
sourcepub fn filters(&self) -> &Vec<Box<dyn Filter + 'static, Global>, Global> ⓘ
pub fn filters(&self) -> &Vec<Box<dyn Filter + 'static, Global>, Global> ⓘ
Get current router’s filters reference.
sourcepub fn filters_mut(
&mut self
) -> &mut Vec<Box<dyn Filter + 'static, Global>, Global> ⓘ
pub fn filters_mut(
&mut self
) -> &mut Vec<Box<dyn Filter + 'static, Global>, Global> ⓘ
Get current router’s filters mutable reference.
sourcepub fn detect(
&self,
req: &mut Request,
path_state: &mut PathState
) -> Option<DetectMatched>
pub fn detect(
&self,
req: &mut Request,
path_state: &mut PathState
) -> Option<DetectMatched>
Detect current router is matched for current request.
sourcepub fn append(self, others: Vec<Router, Global>) -> Router
pub fn append(self, others: Vec<Router, Global>) -> Router
Append all routers in a Vec as children of current router.
sourcepub fn with_hoop<H>(handler: H) -> Routerwhere
H: Handler,
pub fn with_hoop<H>(handler: H) -> Routerwhere
H: Handler,
Add a handler as middleware, it will run the handler in current router or it’s descendants handle the request.
sourcepub fn hoop<H>(self, handler: H) -> Routerwhere
H: Handler,
pub fn hoop<H>(self, handler: H) -> Routerwhere
H: Handler,
Add a handler as middleware, it will run the handler in current router or it’s descendants handle the request.
sourcepub fn with_filter(filter: impl Filter) -> Router
pub fn with_filter(filter: impl Filter) -> Router
Create a new router and set filter.
sourcepub fn with_filter_fn<T>(func: T) -> Routerwhere
T: 'static + Fn(&mut Request, &mut PathState) -> bool + Send + Sync,
pub fn with_filter_fn<T>(func: T) -> Routerwhere
T: 'static + Fn(&mut Request, &mut PathState) -> bool + Send + Sync,
Create a new router and set filter_fn.
sourcepub fn filter_fn<T>(self, func: T) -> Routerwhere
T: 'static + Fn(&mut Request, &mut PathState) -> bool + Send + Sync,
pub fn filter_fn<T>(self, func: T) -> Routerwhere
T: 'static + Fn(&mut Request, &mut PathState) -> bool + Send + Sync,
Create a new FnFilter from Fn.
sourcepub fn then<F>(self, func: F) -> Routerwhere
F: FnOnce(Router) -> Router,
pub fn then<F>(self, func: F) -> Routerwhere
F: FnOnce(Router) -> Router,
When you want write router chain, this function will be useful, You can write your custom logic in FnOnce.
sourcepub fn scheme(self, scheme: Scheme, default: bool) -> Router
pub fn scheme(self, scheme: Scheme, default: bool) -> Router
Add a SchemeFilter
to current router.
sourcepub fn host(self, host: impl Into<String>, default: bool) -> Router
pub fn host(self, host: impl Into<String>, default: bool) -> Router
Add a HostFilter
to current router.
sourcepub fn get<H>(self, handler: H) -> Routerwhere
H: Handler,
pub fn get<H>(self, handler: H) -> Routerwhere
H: Handler,
Create a new child router with MethodFilter
to filter get method and set this child router’s handler.
sourcepub fn post<H>(self, handler: H) -> Routerwhere
H: Handler,
pub fn post<H>(self, handler: H) -> Routerwhere
H: Handler,
Create a new child router with MethodFilter
to filter post method and set this child router’s handler.
sourcepub fn put<H>(self, handler: H) -> Routerwhere
H: Handler,
pub fn put<H>(self, handler: H) -> Routerwhere
H: Handler,
Create a new child router with MethodFilter
to filter put method and set this child router’s handler.
sourcepub fn delete<H>(self, handler: H) -> Routerwhere
H: Handler,
pub fn delete<H>(self, handler: H) -> Routerwhere
H: Handler,
Create a new child router with MethodFilter
to filter delete method and set this child router’s handler.
sourcepub fn patch<H>(self, handler: H) -> Routerwhere
H: Handler,
pub fn patch<H>(self, handler: H) -> Routerwhere
H: Handler,
Create a new child router with MethodFilter
to filter patch method and set this child router’s handler.
sourcepub fn head<H>(self, handler: H) -> Routerwhere
H: Handler,
pub fn head<H>(self, handler: H) -> Routerwhere
H: Handler,
Create a new child router with MethodFilter
to filter head method and set this child router’s handler.
sourcepub fn options<H>(self, handler: H) -> Routerwhere
H: Handler,
pub fn options<H>(self, handler: H) -> Routerwhere
H: Handler,
Create a new child router with MethodFilter
to filter options method and set this child router’s handler.