pub struct Router { /* private fields */ }
Expand description
Generates map between path
and method
which returns ResponseFut
supports *
and :
operators.
Example
let mut router = Router::new();
router.get("/", |req: Request| {
// do not forget to return ResponseFut
});
router.post("/home", |req: Request| {
// do not forget to return ResponseFut
});
// It is important to add default route
// can be simple 404 which will be called if nothing found
router.add_default(|req: Request| {});
let router = router.build();
Implementations§
source§impl Router
impl Router
sourcepub fn build(self) -> Arc<Self>
pub fn build(self) -> Arc<Self>
Wrap router in Arc
to be able to pass it across threads/components.
let mut router = Router::new();
// do some things with router variable
let router = router.build();
// now we can simply clone and call any functions from router instance
let ref_router = router.clone();
sourcepub fn add<F>(&mut self, method: &str, path: &'static str, func: F)where
F: Fn(Request) -> ResponseFut + Send + Sync + 'static,
pub fn add<F>(&mut self, method: &str, path: &'static str, func: F)where
F: Fn(Request) -> ResponseFut + Send + Sync + 'static,
Adds new path -> method
map to the Router.
router.add(method::GET, "/", |req: Request| {});
router.add(method::POST, "/", |req: Request| {});
// and so on
sourcepub fn find(&self, req: Request) -> ResponseFut
pub fn find(&self, req: Request) -> ResponseFut
Searches for appropriate method
which is mapped to specific path
let req: request::Request;
// it will automatically extract path from `req`
router.find(req)
sourcepub fn add_default<F>(&mut self, func: F)where
F: Fn(Request) -> ResponseFut + Send + Sync + 'static,
pub fn add_default<F>(&mut self, func: F)where
F: Fn(Request) -> ResponseFut + Send + Sync + 'static,
Set default function for routes which were not mapped can be simple 404 response.
router.add_default(|req: Request| {});
source§impl Router
impl Router
Abstracts add
method by removing method::*
param