WIP use lambda

This commit is contained in:
AJ ONeal 2018-10-01 19:32:25 -06:00
parent 6ac5399822
commit 00ba212da5
1 changed files with 11 additions and 9 deletions

View File

@ -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();
}
}