feat: basic organization

This commit is contained in:
2025-01-09 21:47:36 +01:00
parent 6b028f822c
commit 2225fa870e
3 changed files with 48 additions and 77 deletions

View File

@@ -1,45 +1,9 @@
use std::fs;
use std::io::prelude::*;
use std::net::TcpStream;
mod generators; mod generators;
mod parsers; mod parsers;
mod server;
mod types; mod types;
use generators::*;
use parsers::*; use parsers::*;
use server::*;
use types::*; use types::*;
pub fn process_petition(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[..]);
let petition = parse_request(&petition);
match petition {
Ok(petition_parsed) => {
let response_status = "200 OK";
let response_content = fs::read_to_string("./routes/index.html").unwrap();
let response: ProcessedResponse = ProcessedResponse {
data: format!(
"HTTP/1.1 {}\r\nContent-Length: {}\r\n\r\n{}",
response_status,
response_content.len(),
response_content
),
status: 200,
};
response
}
Err(error) => {
let response: ProcessedResponse = ProcessedResponse {
data: format!("HTTP/1.1 {}\r\nContent-Length: 0\r\n\r\n", error),
status: error,
};
response
}
}
}

View File

@@ -2,53 +2,57 @@ use std::fs;
use std::io::prelude::*; use std::io::prelude::*;
use std::net::TcpStream; use std::net::TcpStream;
mod generators; use super::*;
mod parsers;
mod types;
use parsers::*; struct HTTPServer<'a> {
use types::*; config: HttpAppConfig,
routes: Vec<HttpAppRoute<'a>>,
struct HTTPServer {
config: ;
routes: ;
} }
impl HTTPServer { impl HTTPServer<'_> {
fn get_route(&self, path: &str) -> Option<&HttpAppRoute> {
self.routes.first() // TODO: search the real one
}
fn process_petition(stream: &mut TcpStream) -> ProcessedResponse { 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[..]);
let petition = parse_request(&petition); let petition = parse_request(&petition);
match petition { match petition {
Ok(petition_parsed) => { Ok(petition_parsed) => {
let response_status = "200 OK"; let response_status = "200 OK";
let response_content = fs::read_to_string("./routes/index.html").unwrap(); let response_content = fs::read_to_string("./routes/index.html").unwrap();
let route = self.get_route(petition_parsed.request.query.path);
let response: ProcessedResponse = ProcessedResponse { if let Some(route) = route {
data: format!( // TODO: call function and generate response
"HTTP/1.1 {}\r\nContent-Length: {}\r\n\r\n{}", } else {
response_status, // TODO: return not found
response_content.len(), }
response_content
),
status: 200,
};
response let response: ProcessedResponse = ProcessedResponse {
} data: format!(
Err(error) => { "HTTP/1.1 {}\r\nContent-Length: {}\r\n\r\n{}",
let response: ProcessedResponse = ProcessedResponse { response_status,
data: format!("HTTP/1.1 {}\r\nContent-Length: 0\r\n\r\n", error), response_content.len(),
status: error, response_content
}; ),
status: 200,
};
response response
}
Err(error) => {
let response: ProcessedResponse = ProcessedResponse {
data: format!("HTTP/1.1 {}\r\nContent-Length: 0\r\n\r\n", error),
status: error,
};
response
}
} }
} }
} }
}

View File

@@ -21,6 +21,9 @@ fn principal() {
} }
} }
// NOTE: example function:
// let response_content = fs::read_to_string("./routes/index.html").unwrap();
pub fn add(left: u64, right: u64) -> u64 { pub fn add(left: u64, right: u64) -> u64 {
left + right left + right
} }