Selaa lähdekoodia

Change API path structure

master
Noelle 4 vuotta sitten
vanhempi
commit
e62263f8fc
1 muutettua tiedostoa jossa 6 lisäystä ja 4 poistoa
  1. 6
    4
      src/main.rs

+ 6
- 4
src/main.rs Näytä tiedosto

@@ -72,8 +72,8 @@ fn handle_connection(mut stream: TcpStream) {
fn route(c: &str, mut r: &mut Response) {

let mut routes = vec![];
routes.push(Route::new("api/hi", api_hi));
routes.push(Route::new("api/bye", api_bye));
routes.push(Route::new("v1/hi", api_hi));
routes.push(Route::new("v1/bye", api_bye));

// println!("Asked to fetch {}", c);
//assert!(Path::new(c).exists());
@@ -82,11 +82,13 @@ fn route(c: &str, mut r: &mut Response) {
r.set_header("HTTP/1.1 200 OK\r\n\r\n");
r.add_file_contents("index.html");
} else if c.starts_with("api/") {
let apipath = c.replace("api/","");
let apipath = apipath.as_str();
let mut iter_routes = routes.into_iter();
let valid_route = iter_routes.find(|x| x.get_path() == c);
let valid_route = iter_routes.find(|x| x.get_path() == apipath);
match valid_route {
Some(rt) => {
(rt.get_action())(&c, &mut r);
(rt.get_action())(&apipath, &mut r);
},
None => {
api_none(&c, &mut r);

Loading…
Peruuta
Tallenna