|
|
@@ -59,7 +59,7 @@ fn handle_connection(mut stream: TcpStream) { |
|
|
|
} |
|
|
|
// println!("Sending {}: {}", r.header, r.path); |
|
|
|
|
|
|
|
let response = format!("{}{}", r.header, r.content); |
|
|
|
let response = format!("{}{}", r.get_header(), r.get_content()); |
|
|
|
|
|
|
|
stream.write(response.as_bytes()).unwrap(); |
|
|
|
stream.flush().unwrap(); |
|
|
@@ -69,22 +69,16 @@ fn handle_connection(mut stream: TcpStream) { |
|
|
|
// println!("Request: {}", String::from_utf8_lossy(&buffer[..])); |
|
|
|
} |
|
|
|
|
|
|
|
fn response_add_file(p: &str, mut r: &mut Response) { |
|
|
|
r.set_content(fs::read_to_string(p).unwrap()); |
|
|
|
fn response_add_file(p: &str, r: &mut Response) { |
|
|
|
r.set_content(fs::read_to_string(p).unwrap().as_str()); |
|
|
|
} |
|
|
|
|
|
|
|
fn route(c: &str, mut r: &mut Response) { |
|
|
|
|
|
|
|
let routes = vec![ |
|
|
|
Route { |
|
|
|
path: "api/hi", |
|
|
|
action: hi, |
|
|
|
}, |
|
|
|
Route { |
|
|
|
path: "api/bye", |
|
|
|
action: bye, |
|
|
|
}, |
|
|
|
]; |
|
|
|
let mut routes = vec![]; |
|
|
|
routes.push(Route::new("api/hi", hi)); |
|
|
|
routes.push(Route::new("api/bye", bye)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// println!("Asked to fetch {}", c); |
|
|
@@ -94,10 +88,11 @@ fn route(c: &str, mut r: &mut Response) { |
|
|
|
r.set_header("HTTP/1.1 200 OK\r\n\r\n"); |
|
|
|
response_add_file("index.html", &mut r); |
|
|
|
} else if c.starts_with("api/") { |
|
|
|
let valid_route = routes.iter().find(|&x| x.path == c); |
|
|
|
let mut iter_routes = routes.into_iter(); |
|
|
|
let valid_route = iter_routes.find(|&&x| x.get_path() == c); |
|
|
|
match valid_route { |
|
|
|
Some(rt) => { |
|
|
|
(rt.action)(&c, &mut r); |
|
|
|
(rt.get_action())(&c, &mut r); |
|
|
|
}, |
|
|
|
None => { |
|
|
|
none_api(&c, &mut r); |