Compare commits
2 Commits
master
...
ref-lambda
Author | SHA1 | Date |
---|---|---|
AJ ONeal | 00ba212da5 | |
AJ ONeal | 6ac5399822 |
19
src/main.rs
19
src/main.rs
|
@ -18,13 +18,24 @@ fn handle_connection(mut stream: TcpStream) {
|
|||
|
||||
stream.read(&mut buffer).unwrap();
|
||||
|
||||
let get = b"GET / HTTP/1.1\r\n";
|
||||
|
||||
println!("Request: {}", String::from_utf8_lossy(&buffer[..]));
|
||||
|
||||
let headers = "HTTP/1.1 200 OK\r\n\r\n".as_bytes();
|
||||
let mut respond = |headers, response| {
|
||||
stream.write(headers).unwrap();
|
||||
let response = fs::read("public/index.html").unwrap();
|
||||
let response = &response[..];
|
||||
|
||||
stream.write(response).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