From 170282cace9a3120373651dea93fbc52b75ea230 Mon Sep 17 00:00:00 2001 From: dqnid Date: Sun, 12 Jan 2025 00:56:07 +0100 Subject: [PATCH] feat: basic server structure done --- src/http/server.rs | 36 ++++++++++++++++++++++++------------ src/http/types.rs | 14 +++++++------- src/lib.rs | 24 ------------------------ 3 files changed, 31 insertions(+), 43 deletions(-) diff --git a/src/http/server.rs b/src/http/server.rs index fe4f287..319be67 100644 --- a/src/http/server.rs +++ b/src/http/server.rs @@ -1,16 +1,11 @@ use std::fs; use std::io::prelude::*; -use std::net::TcpStream; +use std::net::{SocketAddr, TcpListener, TcpStream}; use super::*; -struct HTTPServer<'a> { - config: HttpAppConfig, - routes: Vec>, -} - -impl HTTPServer<'_> { - fn get_route(&self, path: &str) -> Option<&HttpAppRoute> { +impl HttpApp { + fn get_route(&self, _path: &str) -> Option<&HttpAppRoute> { self.routes.first() // TODO: search the real one } @@ -24,11 +19,10 @@ impl HTTPServer<'_> { Ok(petition_parsed) => { let response_status = "200 OK"; - let response_content = fs::read_to_string("./routes/index.html").unwrap(); - let route = self.get_route(petition_parsed.request.query.path); + let mut response_content = fs::read_to_string("./routes/index.html").unwrap(); - if let Some(route) = route { - // TODO: call function and generate response + if let Some(route) = self.get_route(petition_parsed.request.query.path) { + response_content = (route.action)(petition_parsed); } else { // 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(); + } + } } diff --git a/src/http/types.rs b/src/http/types.rs index 6be0836..bb2e66e 100644 --- a/src/http/types.rs +++ b/src/http/types.rs @@ -7,16 +7,16 @@ pub struct HttpAppConfig { port: u8, } -pub type HttpAppRouteFunction = Box String>; +pub type HttpAppRouteFunction = Box String>; -pub struct HttpAppRoute<'a> { - route: &'a str, - action: HttpAppRouteFunction, +pub struct HttpAppRoute { + pub route: String, + pub action: HttpAppRouteFunction, } -pub struct HttpApp<'a> { - config: HttpAppConfig, - routes: Vec>, +pub struct HttpApp { + pub config: HttpAppConfig, + pub routes: Vec, } /* diff --git a/src/lib.rs b/src/lib.rs index 7aa4b75..340c04d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,29 +1,5 @@ -use std::io::prelude::*; -use std::net::{SocketAddr, TcpListener}; - 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 { left + right }