Struct salvo_core::Router
source · 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> ⓘ
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 append(self, others: Vec<Router>) -> Self
pub fn append(self, others: Vec<Router>) -> Self
Append all routers in a Vec as children of current router.
sourcepub fn with_hoop<H: Handler>(handler: H) -> Self
pub fn with_hoop<H: Handler>(handler: 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<H: Handler>(self, handler: H) -> Self
pub fn hoop<H: Handler>(self, handler: 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_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) -> Selfwhere
T: Fn(&mut Request, &mut PathState) -> bool + Send + Sync + 'static,
pub fn with_filter_fn<T>(func: T) -> Selfwhere
T: Fn(&mut Request, &mut PathState) -> bool + Send + Sync + 'static,
Create a new router and set filter_fn.
sourcepub fn filter_fn<T>(self, func: T) -> Selfwhere
T: Fn(&mut Request, &mut PathState) -> bool + Send + Sync + 'static,
pub fn filter_fn<T>(self, func: T) -> Selfwhere
T: Fn(&mut Request, &mut PathState) -> bool + Send + Sync + 'static,
Create a new FnFilter from 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, default: bool) -> Self
pub fn scheme(self, scheme: Scheme, default: bool) -> Self
Add a SchemeFilter
to current router.
sourcepub fn host(self, host: impl Into<String>, default: bool) -> Self
pub fn host(self, host: impl Into<String>, default: bool) -> Self
Add a HostFilter
to current router.
sourcepub fn port(self, port: u16, default: bool) -> Self
pub fn port(self, port: u16, default: bool) -> Self
Add a PortFilter
to current router.
sourcepub fn get<H: Handler>(self, handler: H) -> Self
pub fn get<H: Handler>(self, handler: 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, handler: H) -> Self
pub fn post<H: Handler>(self, handler: 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, handler: H) -> Self
pub fn put<H: Handler>(self, handler: 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, handler: H) -> Self
pub fn delete<H: Handler>(self, handler: 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, handler: H) -> Self
pub fn patch<H: Handler>(self, handler: 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, handler: H) -> Self
pub fn head<H: Handler>(self, handler: 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, handler: H) -> Self
pub fn options<H: Handler>(self, handler: H) -> Self
Create a new child router with MethodFilter
to filter options method and set this child router’s handler.