]> git.proxmox.com Git - proxmox-backup.git/blob - src/api/config.rs
cleanup auth code, verify CSRF prevention token
[proxmox-backup.git] / src / api / config.rs
1 use crate::api::router::*;
2
3 use std::collections::HashMap;
4 use std::path::{PathBuf};
5
6 use hyper::Method;
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: router,
21 aliases: HashMap::new(),
22 env_type,
23 }
24 }
25
26 pub fn find_method(&self, components: &[&str], method: Method, uri_param: &mut HashMap<String, String>) -> &'static MethodDefinition {
27
28 if let Some(info) = self.router.find_route(components, uri_param) {
29 return match method {
30 Method::GET => &info.get,
31 Method::PUT => &info.put,
32 Method::POST => &info.post,
33 Method::DELETE => &info.delete,
34 _ => &MethodDefinition::None,
35 };
36 }
37 &MethodDefinition::None
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]) }
50 } else {
51 for i in 0..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
64 pub fn env_type(&self) -> RpcEnvironmentType {
65 self.env_type
66 }
67 }