pub struct Router { /* private fields */ }Expand description
Router struct, contains routes and the methods needed to route requests to them.
Implementations§
Source§impl Router
impl Router
Sourcepub fn route<F, Fut>(&mut self, method: Method, path: &str, handler: F)
pub fn route<F, Fut>(&mut self, method: Method, path: &str, handler: F)
Appends a new route to a router struct.
Requires a method, path and handler function.
Feature: can take route parameters: /:id, <- id would be the parameter, accessible in Request.params.
§Example:
use zep::{Router, Method, Request, Response};
async fn handler(req: Request) -> Response {
if let Some(name) = req.params.get("name") {
Response::ok(format!("Hello {}!", &name))
} else {
Response::ok("Hello World!")
}
}
let mut router = Router::new();
router.route(Method::GET, "/:name", handler);Sourcepub fn middleware<F, Fut>(&mut self, f: F)
pub fn middleware<F, Fut>(&mut self, f: F)
Appends a middleware to the latest route.
Requires a function with the following signature:
async fn middleware(Request, Handler) -> Response
§Example:
use zep::{Router, Method, Request, Response, Handler};
async fn handler(_req: Request) -> Response {
Response::ok("Hello World!")
}
async fn middleware(req: Request, handler: Handler) -> Response {
//do stuff
return handler(req).await;
}
let mut router = Router::new();
router.route(Method::GET, "/", handler);
router.middleware(middleware);
//middleware is now applied to the `GET /` route.Trait Implementations§
Auto Trait Implementations§
impl Freeze for Router
impl !RefUnwindSafe for Router
impl Send for Router
impl Sync for Router
impl Unpin for Router
impl !UnwindSafe for Router
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more