use crate::HttpStatus;
use crate::request::Request;
use crate::response::Response;
use crate::route::Route;
use crate::server::Handler;
pub struct Router {
routes: Vec<Route>,
}
impl Router {
pub fn new() -> Router {
Router { routes: Vec::new() }
}
pub fn add_route(&mut self, method: &str, path: &str, handler: Handler) {
self.routes.push(Route::new(method, path, handler));
}
pub fn handle(&self, req: &mut Request, res: &mut Response) {
for route in &self.routes {
if route.matches(req) {
(route.handler)(req, res);
return;
}
}
res.status(HttpStatus::NotFound).send("404 Not Found");
}
}