Browse Source

Change API path structure

master
Noelle 4 years ago
parent
commit
e62263f8fc
1 changed files with 6 additions and 4 deletions
  1. 6
    4
      src/main.rs

+ 6
- 4
src/main.rs View File

fn route(c: &str, mut r: &mut Response) { fn route(c: &str, mut r: &mut Response) {


let mut routes = vec![]; 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); // println!("Asked to fetch {}", c);
//assert!(Path::new(c).exists()); //assert!(Path::new(c).exists());
r.set_header("HTTP/1.1 200 OK\r\n\r\n"); r.set_header("HTTP/1.1 200 OK\r\n\r\n");
r.add_file_contents("index.html"); r.add_file_contents("index.html");
} else if c.starts_with("api/") { } else if c.starts_with("api/") {
let apipath = c.replace("api/","");
let apipath = apipath.as_str();
let mut iter_routes = routes.into_iter(); 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 { match valid_route {
Some(rt) => { Some(rt) => {
(rt.get_action())(&c, &mut r);
(rt.get_action())(&apipath, &mut r);
}, },
None => { None => {
api_none(&c, &mut r); api_none(&c, &mut r);

Loading…
Cancel
Save