refactor: lib

This commit is contained in:
2025-01-07 12:24:28 +01:00
parent e006b8dd12
commit 6b028f822c
3 changed files with 54 additions and 0 deletions

54
src/http/server.rs Normal file
View File

@@ -0,0 +1,54 @@
use std::fs;
use std::io::prelude::*;
use std::net::TcpStream;
mod generators;
mod parsers;
mod types;
use parsers::*;
use types::*;
struct HTTPServer {
config: ;
routes: ;
}
impl HTTPServer {
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
}
}
}
}