Add controllers
This commit is contained in:
parent
a392734d59
commit
132917c616
7
src/controller.rs
Normal file
7
src/controller.rs
Normal file
@ -0,0 +1,7 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use tiny_http::Request;
|
||||
|
||||
pub trait Controller {
|
||||
fn register_routes(&self) -> HashMap<String, fn(Request)>;
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
pub mod router;
|
||||
pub mod controller;
|
||||
pub mod entities;
|
||||
pub mod utilities;
|
||||
pub mod template;
|
||||
|
@ -2,7 +2,7 @@ use std::collections::HashMap;
|
||||
|
||||
use tiny_http::Request;
|
||||
|
||||
use crate::{entities::request::Url, utilities::responses::respond_not_found};
|
||||
use crate::{controller::Controller, entities::request::Url, utilities::responses::respond_not_found};
|
||||
|
||||
|
||||
pub struct Router {
|
||||
@ -23,6 +23,14 @@ impl Router {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_controller(&mut self, controller: impl Controller) {
|
||||
let new_routes = controller.register_routes();
|
||||
let mut final_routes = self.routes.clone();
|
||||
final_routes.extend(new_routes);
|
||||
|
||||
self.routes = final_routes;
|
||||
}
|
||||
|
||||
pub fn add_route(&mut self, route: &str, route_function: fn(Request)) {
|
||||
self.routes.insert(route.to_string(), route_function);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user