]> git.proxmox.com Git - proxmox-backup.git/blame - src/api_schema/config.rs
move normalize_path to tools::normalize_uri_path
[proxmox-backup.git] / src / api_schema / config.rs
CommitLineData
dc9a007b 1use crate::api_schema::router::*;
16b48b81
DM
2
3use std::collections::HashMap;
4use std::path::{PathBuf};
5
6use hyper::Method;
7
8pub struct ApiConfig {
9 basedir: PathBuf,
e63e99d6 10 router: &'static Router,
16b48b81 11 aliases: HashMap<String, PathBuf>,
02c7a755 12 env_type: RpcEnvironmentType,
16b48b81
DM
13}
14
15impl ApiConfig {
16
02c7a755 17 pub fn new<B: Into<PathBuf>>(basedir: B, router: &'static Router, env_type: RpcEnvironmentType) -> Self {
16b48b81
DM
18 Self {
19 basedir: basedir.into(),
20 router: router,
21 aliases: HashMap::new(),
02c7a755 22 env_type,
16b48b81
DM
23 }
24 }
25
7e21da6e 26 pub fn find_method(&self, components: &[&str], method: Method, uri_param: &mut HashMap<String, String>) -> &'static MethodDefinition {
16b48b81 27
e7ea17de 28 if let Some(info) = self.router.find_route(components, uri_param) {
7e21da6e 29 return match method {
16b48b81
DM
30 Method::GET => &info.get,
31 Method::PUT => &info.put,
32 Method::POST => &info.post,
33 Method::DELETE => &info.delete,
7e21da6e 34 _ => &MethodDefinition::None,
16b48b81 35 };
16b48b81 36 }
7e21da6e 37 &MethodDefinition::None
16b48b81
DM
38 }
39
40 pub fn find_alias(&self, components: &[&str]) -> PathBuf {
41
42 let mut prefix = String::new();
43 let mut filename = self.basedir.clone();
44 let comp_len = components.len();
45 if comp_len >= 1 {
46 prefix.push_str(components[0]);
47 if let Some(subdir) = self.aliases.get(&prefix) {
48 filename.push(subdir);
49 for i in 1..comp_len { filename.push(components[i]) }
8adbdb0a
DM
50 } else {
51 for i in 0..comp_len { filename.push(components[i]) }
16b48b81
DM
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 }
02c7a755
DM
63
64 pub fn env_type(&self) -> RpcEnvironmentType {
65 self.env_type
66 }
16b48b81 67}