From 700074ce7c06235b6ae17e2a165301f2fe6374ed Mon Sep 17 00:00:00 2001 From: Martin Pitt Date: Fri, 16 Sep 2022 08:12:14 +0200 Subject: [PATCH] async-http: Add logging --- async-http/Cargo.toml | 2 ++ async-http/src/main.rs | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/async-http/Cargo.toml b/async-http/Cargo.toml index 9c26aaa..dfca1b5 100644 --- a/async-http/Cargo.toml +++ b/async-http/Cargo.toml @@ -6,3 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +log = "0.4" +env_logger = "0.9" diff --git a/async-http/src/main.rs b/async-http/src/main.rs index 09ca609..d9e3a26 100644 --- a/async-http/src/main.rs +++ b/async-http/src/main.rs @@ -4,6 +4,8 @@ use std::net::TcpListener; use std::net::TcpStream; fn main() { + env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init(); + // Listen for incoming TCP connections on localhost port 7878 let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); @@ -25,15 +27,16 @@ fn handle_connection(mut stream: TcpStream) { // Respond with greetings or a 404, // depending on the data in the request let (status_line, filename) = if buffer.starts_with(get) { - ("HTTP/1.1 200 OK\r\n\r\n", "index.html") + ("HTTP/1.1 200 OK", "index.html") } else { - ("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html") + ("HTTP/1.1 404 NOT FOUND", "404.html") }; let contents = fs::read_to_string(filename).unwrap(); + log::info!("GET {} {}", filename, status_line); // Write response back to the stream, // and flush the stream to ensure the response is sent back to the client - let response = format!("{status_line}{contents}"); + let response = format!("{status_line}\r\n\r\n{contents}"); stream.write_all(response.as_bytes()).unwrap(); stream.flush().unwrap(); } -- 2.39.5