WIP use lambda
This commit is contained in:
parent
6ac5399822
commit
00ba212da5
24
src/main.rs
24
src/main.rs
|
@ -22,18 +22,20 @@ fn handle_connection(mut stream: TcpStream) {
|
||||||
|
|
||||||
println!("Request: {}", String::from_utf8_lossy(&buffer[..]));
|
println!("Request: {}", String::from_utf8_lossy(&buffer[..]));
|
||||||
|
|
||||||
let (headers, response) = if buffer.starts_with(get) {
|
let mut respond = |headers, response| {
|
||||||
|
|
||||||
let bytes = fs::read("public/index.html").unwrap();
|
|
||||||
("HTTP/1.1 200 OK\r\n\r\n".as_bytes(), bytes[..])
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
("HTTP/1.1 404 NOT FOUND\r\n\r\n".as_bytes(), "Couldn't find {}".as_bytes())
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
stream.write(headers).unwrap();
|
stream.write(headers).unwrap();
|
||||||
stream.write(response).unwrap();
|
stream.write(response).unwrap();
|
||||||
stream.flush().unwrap();
|
stream.flush().unwrap();
|
||||||
|
};
|
||||||
|
|
||||||
|
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 {
|
||||||
|
|
||||||
|
respond("HTTP/1.1 404 NOT FOUND\r\n\r\n".as_bytes(), "Couldn't find {}".as_bytes())
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue