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