diff --git a/src/main.rs b/src/main.rs index f2dfdac..3c0756b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,18 +22,20 @@ fn handle_connection(mut stream: TcpStream) { println!("Request: {}", String::from_utf8_lossy(&buffer[..])); - let (headers, response) = if buffer.starts_with(get) { + let mut respond = |headers, response| { + stream.write(headers).unwrap(); + stream.write(response).unwrap(); + stream.flush().unwrap(); + }; - let bytes = fs::read("public/index.html").unwrap(); - ("HTTP/1.1 200 OK\r\n\r\n".as_bytes(), bytes[..]) + if buffer.starts_with(get) { + + let bytes = fs::read_to_string("public/index.html").unwrap(); + respond("HTTP/1.1 200 OK\r\n\r\n".as_bytes(), bytes.as_bytes()) } else { - ("HTTP/1.1 404 NOT FOUND\r\n\r\n".as_bytes(), "Couldn't find {}".as_bytes()) + respond("HTTP/1.1 404 NOT FOUND\r\n\r\n".as_bytes(), "Couldn't find {}".as_bytes()) - }; - - stream.write(headers).unwrap(); - stream.write(response).unwrap(); - stream.flush().unwrap(); + } }