2021-08-29 16:07:05 +01:00
|
|
|
use std::io::{Write};
|
|
|
|
use std::net::{TcpListener, TcpStream};
|
2021-08-30 16:17:11 +01:00
|
|
|
use std::fs;
|
|
|
|
use std::string::{String};
|
|
|
|
|
|
|
|
fn get_page() -> String {
|
|
|
|
// To-do: Make this able to load other pages
|
|
|
|
//
|
|
|
|
// The loaded page should be left immutable as it does
|
|
|
|
// not need to be modified by the server.
|
|
|
|
|
|
|
|
let filename = "index.html"; // BAD!!!
|
|
|
|
let page = fs::read_to_string(filename)
|
|
|
|
.expect("Page read failed!");
|
|
|
|
|
|
|
|
return page;
|
|
|
|
}
|
2021-08-29 16:07:05 +01:00
|
|
|
|
|
|
|
fn serve(mut stream: TcpStream) {
|
2021-08-30 16:17:11 +01:00
|
|
|
// To-do: Make a header generator to feed into response.
|
|
|
|
|
|
|
|
let contents = get_page();
|
|
|
|
let header = "HTTP/1.1 200 OK\r\n\r\n";
|
|
|
|
let response = format!("{}{}", header, contents);
|
|
|
|
|
2021-08-29 16:07:05 +01:00
|
|
|
stream.write(response.as_bytes()).unwrap();
|
|
|
|
stream.flush().unwrap();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn main() -> std::io::Result<()> {
|
|
|
|
let listen = TcpListener::bind("127.0.0.1:8080")?;
|
|
|
|
|
|
|
|
for stream in listen.incoming() {
|
|
|
|
serve(stream?);
|
|
|
|
}
|
|
|
|
Ok(())
|
2021-08-29 15:18:20 +01:00
|
|
|
}
|