WIP use lambda
This commit is contained in:
parent
6ac5399822
commit
00ba212da5
20
src/main.rs
20
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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue