diff --git a/docs/TODO.md b/docs/TODO.md index 493c7fb..c23ac0b 100644 --- a/docs/TODO.md +++ b/docs/TODO.md @@ -1,6 +1,6 @@ # TODO -- [ ] Manage requests -- [ ] Router +- [x] Manage requests +- [x] Router - [NOT NEEDED] HTTP code mapper : not needed because a code will be directly translated on the client (browser / postman) - [ ] JS / CSS data - [ ] Media manager @@ -8,6 +8,8 @@ - [ ] File management - [ ] Auto-cleanup - [ ] Transversal utility +- [ ] Let programmer set the default not found response +- [ ] Allow middleware ## Improvements diff --git a/src/http/mod.rs b/src/http/mod.rs index 1e7a922..7a8bf2b 100644 --- a/src/http/mod.rs +++ b/src/http/mod.rs @@ -5,5 +5,5 @@ mod types; use generators::*; use parsers::*; -use server::*; -use types::*; +pub use server::*; +pub use types::*; diff --git a/src/http/server.rs b/src/http/server.rs index 319be67..6126300 100644 --- a/src/http/server.rs +++ b/src/http/server.rs @@ -9,7 +9,11 @@ impl HttpApp { self.routes.first() // TODO: search the real one } - fn process_petition(&self, stream: &mut TcpStream) -> ProcessedResponse { + pub fn add_route(&mut self, route: HttpAppRoute) { + self.routes.push(route); + } + + pub fn process_petition(&self, stream: &mut TcpStream) -> ProcessedResponse { let mut buffer = [0; 1024]; // TODO: manage this size let _amount = stream.read(&mut buffer); let petition = String::from_utf8_lossy(&buffer[..]); @@ -19,7 +23,8 @@ impl HttpApp { Ok(petition_parsed) => { let response_status = "200 OK"; - let mut response_content = fs::read_to_string("./routes/index.html").unwrap(); + // let mut response_content = fs::read_to_string("./routes/index.html").unwrap(); + let mut response_content = "".to_string(); if let Some(route) = self.get_route(petition_parsed.request.query.path) { response_content = (route.action)(petition_parsed); @@ -50,8 +55,8 @@ impl HttpApp { } } - fn start(&self) { - let addr = SocketAddr::from(([127, 0, 0, 1], 80)); + pub fn start(&self) { + let addr = SocketAddr::from(([127, 0, 0, 1], self.config.port)); let listener = TcpListener::bind(addr).unwrap(); diff --git a/src/http/types.rs b/src/http/types.rs index bb2e66e..88c3904 100644 --- a/src/http/types.rs +++ b/src/http/types.rs @@ -4,7 +4,7 @@ use std::collections::HashMap; * App types * */ pub struct HttpAppConfig { - port: u8, + pub port: u16, } pub type HttpAppRouteFunction = Box String>; diff --git a/src/lib.rs b/src/lib.rs index 340c04d..f5bad0f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,7 @@ mod http; +pub use http::*; + pub fn add(left: u64, right: u64) -> u64 { left + right }