Move high level logic from ear to lib
This commit is contained in:
parent
1d0d8fdba8
commit
f37b6e1b63
2 changed files with 33 additions and 25 deletions
32
src/ear.rs
32
src/ear.rs
|
@ -45,6 +45,7 @@ impl Display for HttpMethod {
|
|||
pub struct Ear {
|
||||
port: u32,
|
||||
ip_addr: String,
|
||||
pub listener: Option<TcpListener>,
|
||||
}
|
||||
|
||||
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<String> {
|
||||
pub fn extract_first_line_of_request(mut stream: &TcpStream) -> Option<String> {
|
||||
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<String> {
|
|||
}
|
||||
|
||||
/// Receives and parses a request to dermine the function to call.
|
||||
fn parse_uri(full_request: &str) -> Result<Request, ReqParseError> {
|
||||
pub fn parse_uri(full_request: &str) -> Result<Request, ReqParseError> {
|
||||
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<Request, ReqParseError> {
|
|||
}
|
||||
|
||||
/// 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;
|
||||
|
|
26
src/lib.rs
26
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<dyn Error>> {
|
||||
|
@ -37,8 +38,29 @@ pub fn run() -> Result<(), Box<dyn Error>> {
|
|||
});
|
||||
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(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue