pub struct Router { /* private fields */ }
Expand description
Simple mapper which maps different route to the corresponding functions which will be executed in case of match
Example
let mut router = Router::new();
router.get("/", hello_world);
router.get("/home", home);
// It is important to add default route
router.add_default(not_found);
let router = router.build();
Implementations§
source§impl Router
impl Router
pub fn new() -> Router
sourcepub fn build(self) -> Arc<Self>
pub fn build(self) -> Arc<Self>
build wraps router in Arc so that we can copy ref pointer in to the tokio stream and fold functions
sourcepub fn find(&self, req: Request) -> ReturnFuture
pub fn find(&self, req: Request) -> ReturnFuture
Is used to find appropriate Node instance in Route map and return Boxed future
example find(req)
will return future which can be chained and executed
sourcepub fn add<F>(&mut self, method: &str, path: &'static str, func: F)where
F: Fn(Request) -> Box<dyn Future<Item = Response, Error = ()> + Send + Sync> + Send + Sync + 'static,
pub fn add<F>(&mut self, method: &str, path: &'static str, func: F)where
F: Fn(Request) -> Box<dyn Future<Item = Response, Error = ()> + Send + Sync> + Send + Sync + 'static,
Create and adds Node instances to the Router, can be used to attach
different requests to the router for example router.add(method::PATCH, path, func)
source§impl Router
impl Router
Router helpers methods to simplify route creation
sourcepub fn get<F>(&mut self, path: &'static str, func: F)where
F: Fn(Request) -> Box<dyn Future<Item = Response, Error = ()> + Send + Sync> + Send + Sync + 'static,
pub fn get<F>(&mut self, path: &'static str, func: F)where
F: Fn(Request) -> Box<dyn Future<Item = Response, Error = ()> + Send + Sync> + Send + Sync + 'static,
Helper for add
method which simplifies creation of get
router
instead of router.add(method::GET, path, func)
you can write
router.get(path, func)
sourcepub fn post<F>(&mut self, path: &'static str, func: F)where
F: Fn(Request) -> Box<dyn Future<Item = Response, Error = ()> + Send + Sync> + Send + Sync + 'static,
pub fn post<F>(&mut self, path: &'static str, func: F)where
F: Fn(Request) -> Box<dyn Future<Item = Response, Error = ()> + Send + Sync> + Send + Sync + 'static,
Helper for add
method which simplifies creation of post
router
instead of router.add(method::POST, path, func)
you can write
router.post(path, func)
sourcepub fn put<F>(&mut self, path: &'static str, func: F)where
F: Fn(Request) -> Box<dyn Future<Item = Response, Error = ()> + Send + Sync> + Send + Sync + 'static,
pub fn put<F>(&mut self, path: &'static str, func: F)where
F: Fn(Request) -> Box<dyn Future<Item = Response, Error = ()> + Send + Sync> + Send + Sync + 'static,
Helper for add
method which simplifies creation of put
router
instead of router.add(method::PUT, path, func)
you can write
router.put(path, func)
sourcepub fn head<F>(&mut self, path: &'static str, func: F)where
F: Fn(Request) -> Box<dyn Future<Item = Response, Error = ()> + Send + Sync> + Send + Sync + 'static,
pub fn head<F>(&mut self, path: &'static str, func: F)where
F: Fn(Request) -> Box<dyn Future<Item = Response, Error = ()> + Send + Sync> + Send + Sync + 'static,
Helper for add
method which simplifies creation of head
router
instead of router.add(method::HEAD, path, func)
you can write
router.head(path, func)
sourcepub fn patch<F>(&mut self, path: &'static str, func: F)where
F: Fn(Request) -> Box<dyn Future<Item = Response, Error = ()> + Send + Sync> + Send + Sync + 'static,
pub fn patch<F>(&mut self, path: &'static str, func: F)where
F: Fn(Request) -> Box<dyn Future<Item = Response, Error = ()> + Send + Sync> + Send + Sync + 'static,
Helper for add
method which simplifies creation of patch
router
instead of router.add(method::PATCH, path, func)
you can write
router.patch(path, func)
sourcepub fn delete<F>(&mut self, path: &'static str, func: F)where
F: Fn(Request) -> Box<dyn Future<Item = Response, Error = ()> + Send + Sync> + Send + Sync + 'static,
pub fn delete<F>(&mut self, path: &'static str, func: F)where
F: Fn(Request) -> Box<dyn Future<Item = Response, Error = ()> + Send + Sync> + Send + Sync + 'static,
Helper for add
method which simplifies creation of delete
router
instead of router.add(method::DELETE, path, func)
you can write
router.delete(path, func)
sourcepub fn options<F>(&mut self, path: &'static str, func: F)where
F: Fn(Request) -> Box<dyn Future<Item = Response, Error = ()> + Send + Sync> + Send + Sync + 'static,
pub fn options<F>(&mut self, path: &'static str, func: F)where
F: Fn(Request) -> Box<dyn Future<Item = Response, Error = ()> + Send + Sync> + Send + Sync + 'static,
Helper for add
method which simplifies creation of options
router
instead of router.add(method::OPTIONS, path, func)
you can write
router.options(path, func)
sourcepub fn add_default<F>(&mut self, func: F)where
F: Fn(Request) -> Box<dyn Future<Item = Response, Error = ()> + Send + Sync> + Send + Sync + 'static,
pub fn add_default<F>(&mut self, func: F)where
F: Fn(Request) -> Box<dyn Future<Item = Response, Error = ()> + Send + Sync> + Send + Sync + 'static,
Adds default method to the Route instance, default method is executed when no matching route found can be used to response with 404 error It is mandatory to set default method