[RELEASE] Version 0.0.7 #9

Merged
jank merged 9 commits from feature/version-0.0.7 into main 2024-08-21 14:04:27 +00:00
3 changed files with 17 additions and 1 deletions
Showing only changes of commit 132917c616 - Show all commits

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);
}