feat: basic server structure done

This commit is contained in:
2025-01-12 00:56:07 +01:00
parent 2225fa870e
commit 170282cace
3 changed files with 31 additions and 43 deletions

View File

@@ -1,16 +1,11 @@
use std::fs; use std::fs;
use std::io::prelude::*; use std::io::prelude::*;
use std::net::TcpStream; use std::net::{SocketAddr, TcpListener, TcpStream};
use super::*; use super::*;
struct HTTPServer<'a> { impl HttpApp {
config: HttpAppConfig, fn get_route(&self, _path: &str) -> Option<&HttpAppRoute> {
routes: Vec<HttpAppRoute<'a>>,
}
impl HTTPServer<'_> {
fn get_route(&self, path: &str) -> Option<&HttpAppRoute> {
self.routes.first() // TODO: search the real one self.routes.first() // TODO: search the real one
} }
@@ -24,11 +19,10 @@ impl HTTPServer<'_> {
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 mut response_content = fs::read_to_string("./routes/index.html").unwrap();
let route = self.get_route(petition_parsed.request.query.path);
if let Some(route) = route { if let Some(route) = self.get_route(petition_parsed.request.query.path) {
// TODO: call function and generate response response_content = (route.action)(petition_parsed);
} else { } else {
// TODO: return not found // TODO: return not found
} }
@@ -55,4 +49,22 @@ impl HTTPServer<'_> {
} }
} }
} }
fn start(&self) {
let addr = SocketAddr::from(([127, 0, 0, 1], 80));
let listener = TcpListener::bind(addr).unwrap();
println!("Server up and running!");
for stream in listener.incoming() {
let mut _stream = stream.unwrap();
println!("Connection established!");
let response = self.process_petition(&mut _stream);
// TODO: manage error case
let _amount = _stream.write(response.data.as_bytes()).unwrap();
_stream.flush().unwrap();
}
}
} }

View File

@@ -7,16 +7,16 @@ pub struct HttpAppConfig {
port: u8, port: u8,
} }
pub type HttpAppRouteFunction = Box<dyn Fn(HttpRequest) -> String>; pub type HttpAppRouteFunction = Box<fn(HttpRequest) -> String>;
pub struct HttpAppRoute<'a> { pub struct HttpAppRoute {
route: &'a str, pub route: String,
action: HttpAppRouteFunction, pub action: HttpAppRouteFunction,
} }
pub struct HttpApp<'a> { pub struct HttpApp {
config: HttpAppConfig, pub config: HttpAppConfig,
routes: Vec<HttpAppRoute<'a>>, pub routes: Vec<HttpAppRoute>,
} }
/* /*

View File

@@ -1,29 +1,5 @@
use std::io::prelude::*;
use std::net::{SocketAddr, TcpListener};
mod http; mod http;
fn principal() {
let addr = SocketAddr::from(([127, 0, 0, 1], 80));
let listener = TcpListener::bind(addr).unwrap();
println!("Server up and running!");
for stream in listener.incoming() {
let mut _stream = stream.unwrap();
println!("Connection established!");
let response = http::process_petition(&mut _stream);
// TODO: manage error case
let _amount = _stream.write(response.data.as_bytes()).unwrap();
_stream.flush().unwrap();
}
}
// 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
} }