]>
Commit | Line | Data |
---|---|---|
1a53be14 DM |
1 | use std::collections::HashMap; |
2 | use std::path::{PathBuf}; | |
3 | use crate::api_info::*; | |
08bd8d47 | 4 | //use crate::json_schema::*; |
1a53be14 | 5 | |
08bd8d47 | 6 | use hyper::{Method}; |
1a53be14 DM |
7 | |
8 | pub struct ApiServer { | |
9 | basedir: PathBuf, | |
10 | router: &'static MethodInfo, | |
11 | aliases: HashMap<String, PathBuf>, | |
12 | } | |
13 | ||
14 | impl ApiServer { | |
15 | ||
16 | pub fn new<B: Into<PathBuf>>(basedir: B, router: &'static MethodInfo) -> Self { | |
17 | ApiServer { | |
18 | basedir: basedir.into(), | |
19 | router: router, | |
20 | aliases: HashMap::new(), | |
21 | } | |
22 | } | |
23 | ||
24 | pub fn find_method(&self, components: &[&str], method: Method) -> Option<&'static ApiMethod> { | |
25 | ||
26 | if let Some(info) = self.router.find_route(components) { | |
27 | println!("FOUND INFO"); | |
28 | let opt_api_method = match method { | |
29 | Method::GET => &info.get, | |
30 | Method::PUT => &info.put, | |
31 | Method::POST => &info.post, | |
32 | Method::DELETE => &info.delete, | |
33 | _ => &None, | |
34 | }; | |
35 | if let Some(api_method) = opt_api_method { | |
36 | return Some(&api_method); | |
37 | } | |
38 | } | |
39 | None | |
40 | } | |
41 | ||
42 | pub fn find_alias(&self, components: &[&str]) -> PathBuf { | |
43 | ||
44 | let mut prefix = String::new(); | |
45 | let mut filename = self.basedir.clone(); | |
46 | let comp_len = components.len(); | |
47 | if comp_len >= 1 { | |
48 | prefix.push_str(components[0]); | |
49 | if let Some(subdir) = self.aliases.get(&prefix) { | |
50 | filename.push(subdir); | |
51 | for i in 1..comp_len { filename.push(components[i]) } | |
52 | } | |
53 | } | |
54 | filename | |
55 | } | |
56 | ||
57 | pub fn add_alias<S, P>(&mut self, alias: S, path: P) | |
58 | where S: Into<String>, | |
59 | P: Into<PathBuf>, | |
60 | { | |
61 | self.aliases.insert(alias.into(), path.into()); | |
62 | } | |
63 | } |