feat(library): basic library structure completed

This commit is contained in:
2025-01-14 21:45:40 +01:00
parent 170282cace
commit 3160febb58
5 changed files with 18 additions and 9 deletions

View File

@@ -1,6 +1,6 @@
# TODO # TODO
- [ ] Manage requests - [x] Manage requests
- [ ] Router - [x] Router
- [NOT NEEDED] HTTP code mapper : not needed because a code will be directly translated on the client (browser / postman) - [NOT NEEDED] HTTP code mapper : not needed because a code will be directly translated on the client (browser / postman)
- [ ] JS / CSS data - [ ] JS / CSS data
- [ ] Media manager - [ ] Media manager
@@ -8,6 +8,8 @@
- [ ] File management - [ ] File management
- [ ] Auto-cleanup - [ ] Auto-cleanup
- [ ] Transversal utility - [ ] Transversal utility
- [ ] Let programmer set the default not found response
- [ ] Allow middleware
## Improvements ## Improvements

View File

@@ -5,5 +5,5 @@ mod types;
use generators::*; use generators::*;
use parsers::*; use parsers::*;
use server::*; pub use server::*;
use types::*; pub use types::*;

View File

@@ -9,7 +9,11 @@ impl HttpApp {
self.routes.first() // TODO: search the real one 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 mut buffer = [0; 1024]; // TODO: manage this size
let _amount = stream.read(&mut buffer); let _amount = stream.read(&mut buffer);
let petition = String::from_utf8_lossy(&buffer[..]); let petition = String::from_utf8_lossy(&buffer[..]);
@@ -19,7 +23,8 @@ impl HttpApp {
Ok(petition_parsed) => { Ok(petition_parsed) => {
let response_status = "200 OK"; 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) { if let Some(route) = self.get_route(petition_parsed.request.query.path) {
response_content = (route.action)(petition_parsed); response_content = (route.action)(petition_parsed);
@@ -50,8 +55,8 @@ impl HttpApp {
} }
} }
fn start(&self) { pub fn start(&self) {
let addr = SocketAddr::from(([127, 0, 0, 1], 80)); let addr = SocketAddr::from(([127, 0, 0, 1], self.config.port));
let listener = TcpListener::bind(addr).unwrap(); let listener = TcpListener::bind(addr).unwrap();

View File

@@ -4,7 +4,7 @@ use std::collections::HashMap;
* App types * App types
* */ * */
pub struct HttpAppConfig { pub struct HttpAppConfig {
port: u8, pub port: u16,
} }
pub type HttpAppRouteFunction = Box<fn(HttpRequest) -> String>; pub type HttpAppRouteFunction = Box<fn(HttpRequest) -> String>;

View File

@@ -1,5 +1,7 @@
mod http; mod http;
pub use http::*;
pub fn add(left: u64, right: u64) -> u64 { pub fn add(left: u64, right: u64) -> u64 {
left + right left + right
} }