Add controllers
All checks were successful
Cargo Build & Test / Tests (pull_request) Successful in 11s
Cargo Build & Test / Test publish (pull_request) Successful in 12s
Cargo Build & Test / check-cargo-version (pull_request) Successful in 5s

This commit is contained in:
Jan Gleytenhoover 2024-08-21 08:12:12 +02:00
parent a392734d59
commit 132917c616
Signed by: jank
GPG Key ID: B267751B8AE29EFE
3 changed files with 17 additions and 1 deletions

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 router;
pub mod controller;
pub mod entities; pub mod entities;
pub mod utilities; pub mod utilities;
pub mod template; pub mod template;

@ -2,7 +2,7 @@ use std::collections::HashMap;
use tiny_http::Request; 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 { 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)) { pub fn add_route(&mut self, route: &str, route_function: fn(Request)) {
self.routes.insert(route.to_string(), route_function); self.routes.insert(route.to_string(), route_function);
} }