|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
// println!("Sending {}: {}", r.header, r.path); |
|
|
// 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.write(response.as_bytes()).unwrap(); |
|
|
stream.flush().unwrap(); |
|
|
stream.flush().unwrap(); |
|
|
|
|
|
|
|
|
// println!("Request: {}", String::from_utf8_lossy(&buffer[..])); |
|
|
// 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) { |
|
|
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); |
|
|
// println!("Asked to fetch {}", c); |
|
|
|
|
|
|
|
|
r.set_header("HTTP/1.1 200 OK\r\n\r\n"); |
|
|
r.set_header("HTTP/1.1 200 OK\r\n\r\n"); |
|
|
response_add_file("index.html", &mut r); |
|
|
response_add_file("index.html", &mut r); |
|
|
} else if c.starts_with("api/") { |
|
|
} 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 { |
|
|
match valid_route { |
|
|
Some(rt) => { |
|
|
Some(rt) => { |
|
|
(rt.action)(&c, &mut r); |
|
|
|
|
|
|
|
|
(rt.get_action())(&c, &mut r); |
|
|
}, |
|
|
}, |
|
|
None => { |
|
|
None => { |
|
|
none_api(&c, &mut r); |
|
|
none_api(&c, &mut r); |