From f37b6e1b63357326808c008922f6df7c7500af17 Mon Sep 17 00:00:00 2001 From: "flyingscorpio@clevo" Date: Tue, 10 Jan 2023 21:47:00 +0100 Subject: [PATCH] Move high level logic from ear to lib --- src/ear.rs | 32 +++++++++----------------------- src/lib.rs | 26 ++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/ear.rs b/src/ear.rs index 3a7fe8c..6edaf9b 100644 --- a/src/ear.rs +++ b/src/ear.rs @@ -45,6 +45,7 @@ impl Display for HttpMethod { pub struct Ear { port: u32, ip_addr: String, + pub listener: Option, } impl Ear { @@ -52,36 +53,21 @@ impl Ear { Self { port: 8080, ip_addr: String::from("0.0.0.0"), + listener: None, } } /// Main event listener for the web server. - pub fn listen(&self) { - let listener = TcpListener::bind(format!("{}:{}", self.ip_addr, self.port)).expect("Could not bind"); + pub fn listen(&mut self) { + self.listener = Some( + TcpListener::bind(format!("{}:{}", self.ip_addr, self.port)).expect("Could not bind"), + ); println!("Listening on {}:{}", self.ip_addr, self.port); - - for stream in listener.incoming() { - let stream = match stream { - Ok(stream) => stream, - Err(_) => continue, - }; - let req = match extract_first_line_of_request(&stream) { - Some(req) => req, - None => continue, - }; - println!("Received request: {}", &req); - let mut sender = Mouth::new(&stream); - - match parse_uri(&req) { - Ok(req) => handle_request(req, sender), - Err(req) => sender.send_bad_request(format!("{}", req)), - }; - } } } /// Reads a stream in a buffer and extracts the first line. -fn extract_first_line_of_request(mut stream: &TcpStream) -> Option { +pub fn extract_first_line_of_request(mut stream: &TcpStream) -> Option { let buf_reader = BufReader::new(&mut stream); let http_request = buf_reader.lines().next()?; match http_request { @@ -91,7 +77,7 @@ fn extract_first_line_of_request(mut stream: &TcpStream) -> Option { } /// Receives and parses a request to dermine the function to call. -fn parse_uri(full_request: &str) -> Result { +pub fn parse_uri(full_request: &str) -> Result { let method = match full_request.split_whitespace().nth(0) { Some("GET") => HttpMethod::Get, Some("POST") => HttpMethod::Post, @@ -128,7 +114,7 @@ fn parse_uri(full_request: &str) -> Result { } /// Handles a given well formed request -fn handle_request(request: Request, sender: Mouth) { +pub fn handle_request(request: Request, sender: Mouth) { database::save_request_to_history(&request).unwrap_or_else(|err| { eprintln!("{err}"); return; diff --git a/src/lib.rs b/src/lib.rs index ff91184..2c53192 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,6 +13,7 @@ mod schema; use api_client::ApiClient; use ear::Ear; +use mouth::Mouth; /// Entry point of the library, propagates errors to `main`. pub fn run() -> Result<(), Box> { @@ -37,8 +38,29 @@ pub fn run() -> Result<(), Box> { }); println!("Done!"); - let listener = Ear::new(); - listener.listen(); + let mut ear = Ear::new(); + ear.listen(); + + if let Some(listener) = ear.listener { + for stream in listener.incoming() { + let stream = match stream { + Ok(stream) => stream, + Err(_) => continue, + }; + let req = match ear::extract_first_line_of_request(&stream) { + Some(req) => req, + None => continue, + }; + println!("Received request: {}", &req); + + let mut sender = Mouth::new(&stream); + + match ear::parse_uri(&req) { + Ok(req) => ear::handle_request(req, sender), + Err(req) => sender.send_bad_request(format!("{}", req)), + }; + } + } Ok(()) }