]> git.proxmox.com Git - proxmox-backup.git/blob - src/api_schema/config.rs
d9aa076c40eef5d22685db853fa29c9b9f0cbbbc
[proxmox-backup.git] / src / api_schema / config.rs
1 use std::collections::HashMap;
2 use std::path::{PathBuf};
3
4 use hyper::Method;
5
6 use proxmox::api::{ApiMethod, Router, RpcEnvironmentType};
7
8 pub struct ApiConfig {
9 basedir: PathBuf,
10 router: &'static Router,
11 aliases: HashMap<String, PathBuf>,
12 env_type: RpcEnvironmentType,
13 }
14
15 impl ApiConfig {
16
17 pub fn new<B: Into<PathBuf>>(basedir: B, router: &'static Router, env_type: RpcEnvironmentType) -> Self {
18 Self {
19 basedir: basedir.into(),
20 router,
21 aliases: HashMap::new(),
22 env_type,
23 }
24 }
25
26 pub fn find_method(
27 &self,
28 components: &[&str],
29 method: Method,
30 uri_param: &mut HashMap<String, String>,
31 ) -> Option<&'static ApiMethod> {
32
33 self.router.find_method(components, method, uri_param)
34 }
35
36 pub fn find_alias(&self, components: &[&str]) -> PathBuf {
37
38 let mut prefix = String::new();
39 let mut filename = self.basedir.clone();
40 let comp_len = components.len();
41 if comp_len >= 1 {
42 prefix.push_str(components[0]);
43 if let Some(subdir) = self.aliases.get(&prefix) {
44 filename.push(subdir);
45 for i in 1..comp_len { filename.push(components[i]) }
46 } else {
47 for i in 0..comp_len { filename.push(components[i]) }
48 }
49 }
50 filename
51 }
52
53 pub fn add_alias<S, P>(&mut self, alias: S, path: P)
54 where S: Into<String>,
55 P: Into<PathBuf>,
56 {
57 self.aliases.insert(alias.into(), path.into());
58 }
59
60 pub fn env_type(&self) -> RpcEnvironmentType {
61 self.env_type
62 }
63 }