Struct salvo_core::routing::Router
source · #[non_exhaustive]pub struct Router {
pub routers: Vec<Router>,
pub filters: Vec<Box<dyn Filter>>,
pub hoops: Vec<Arc<dyn Handler>>,
pub goal: Option<Arc<dyn Handler>>,
/* private fields */
}
Expand description
Router struct is used for route request to different handlers.
You can write 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.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. }
syntax; cannot be matched against without a wildcard ..
; and struct update syntax will not work.routers: Vec<Router>
The children of current router.
filters: Vec<Box<dyn Filter>>
The filters of current router.
hoops: Vec<Arc<dyn Handler>>
The middlewares of current router.
goal: Option<Arc<dyn Handler>>
The final handler to handle request of current router.
Implementations§
source§impl Router
impl Router
sourcepub fn routers_mut(&mut self) -> &mut Vec<Router>
pub fn routers_mut(&mut self) -> &mut Vec<Router>
Get current router’s children mutable reference.
sourcepub fn hoops_mut(&mut self) -> &mut Vec<Arc<dyn Handler>>
pub fn hoops_mut(&mut self) -> &mut Vec<Arc<dyn Handler>>
Get current router’s middlewares mutable reference.
sourcepub fn filters_mut(&mut self) -> &mut Vec<Box<dyn Filter>>
pub fn filters_mut(&mut self) -> &mut Vec<Box<dyn Filter>>
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 unshift(self, router: Router) -> Self
pub fn unshift(self, router: Router) -> Self
Insert a router at the begining of current router, shifting all routers after it to the right.
sourcepub fn insert(self, index: usize, router: Router) -> Self
pub fn insert(self, index: usize, router: Router) -> Self
Insert a router at position index
within current router, shifting all routers after it to the right.
sourcepub fn append(self, others: &mut Vec<Router>) -> Self
pub fn append(self, others: &mut Vec<Router>) -> Self
Append all routers in a Vec as children of current router.
sourcepub fn with_hoop<H: Handler>(hoop: H) -> Self
pub fn with_hoop<H: Handler>(hoop: H) -> Self
Add a handler as middleware, it will run the handler in current router or it’s descendants handle the request.
sourcepub fn with_hoop_when<H, F>(hoop: H, filter: F) -> Self
pub fn with_hoop_when<H, F>(hoop: H, filter: F) -> Self
Add a handler as middleware, it will run the handler in current router or it’s descendants handle the request. This middleware only effective when the filter return true.
sourcepub fn hoop<H: Handler>(self, hoop: H) -> Self
pub fn hoop<H: Handler>(self, hoop: H) -> Self
Add a handler as middleware, it will run the handler in current router or it’s descendants handle the request.
sourcepub fn hoop_when<H, F>(self, hoop: H, filter: F) -> Self
pub fn hoop_when<H, F>(self, hoop: H, filter: F) -> Self
Add a handler as middleware, it will run the handler in current router or it’s descendants handle the request. This middleware only effective when the filter return true.
sourcepub fn with_filter(filter: impl Filter + Sized) -> Self
pub fn with_filter(filter: impl Filter + Sized) -> Self
Create a new router and set filter.
sourcepub fn with_filter_fn<T>(func: T) -> Self
pub fn with_filter_fn<T>(func: T) -> Self
Create a new router and set filter_fn.
sourcepub fn then<F>(self, func: F) -> Selfwhere
F: FnOnce(Self) -> Self,
pub fn then<F>(self, func: F) -> Selfwhere
F: FnOnce(Self) -> Self,
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) -> Self
pub fn scheme(self, scheme: Scheme) -> Self
Add a SchemeFilter
to current router.
sourcepub fn host(self, host: impl Into<String>) -> Self
pub fn host(self, host: impl Into<String>) -> Self
Add a HostFilter
to current router.
sourcepub fn port(self, port: u16) -> Self
pub fn port(self, port: u16) -> Self
Add a PortFilter
to current router.
sourcepub fn get<H: Handler>(self, goal: H) -> Self
pub fn get<H: Handler>(self, goal: H) -> Self
Create a new child router with MethodFilter
to filter get method and set this child router’s handler.
sourcepub fn post<H: Handler>(self, goal: H) -> Self
pub fn post<H: Handler>(self, goal: H) -> Self
Create a new child router with MethodFilter
to filter post method and set this child router’s handler.
sourcepub fn put<H: Handler>(self, goal: H) -> Self
pub fn put<H: Handler>(self, goal: H) -> Self
Create a new child router with MethodFilter
to filter put method and set this child router’s handler.
sourcepub fn delete<H: Handler>(self, goal: H) -> Self
pub fn delete<H: Handler>(self, goal: H) -> Self
Create a new child router with MethodFilter
to filter delete method and set this child router’s handler.
sourcepub fn patch<H: Handler>(self, goal: H) -> Self
pub fn patch<H: Handler>(self, goal: H) -> Self
Create a new child router with MethodFilter
to filter patch method and set this child router’s handler.
sourcepub fn head<H: Handler>(self, goal: H) -> Self
pub fn head<H: Handler>(self, goal: H) -> Self
Create a new child router with MethodFilter
to filter head method and set this child router’s handler.
sourcepub fn options<H: Handler>(self, goal: H) -> Self
pub fn options<H: Handler>(self, goal: H) -> Self
Create a new child router with MethodFilter
to filter options method and set this child router’s handler.