]> git.proxmox.com Git - proxmox-backup.git/blame - src/server/config.rs
tools file logger: fix example and comments
[proxmox-backup.git] / src / server / config.rs
CommitLineData
16b48b81 1use std::collections::HashMap;
2ab5acac
DC
2use std::path::PathBuf;
3use std::time::SystemTime;
4use std::fs::metadata;
5use std::sync::RwLock;
16b48b81 6
2ab5acac 7use anyhow::{bail, Error, format_err};
16b48b81 8use hyper::Method;
f9e3b110 9use handlebars::Handlebars;
2ab5acac 10use serde::Serialize;
16b48b81 11
a2479cfa
WB
12use proxmox::api::{ApiMethod, Router, RpcEnvironmentType};
13
16b48b81
DM
14pub struct ApiConfig {
15 basedir: PathBuf,
e63e99d6 16 router: &'static Router,
16b48b81 17 aliases: HashMap<String, PathBuf>,
02c7a755 18 env_type: RpcEnvironmentType,
2ab5acac
DC
19 templates: RwLock<Handlebars<'static>>,
20 template_files: RwLock<HashMap<String, (SystemTime, PathBuf)>>,
16b48b81
DM
21}
22
23impl ApiConfig {
24
f9e3b110 25 pub fn new<B: Into<PathBuf>>(basedir: B, router: &'static Router, env_type: RpcEnvironmentType) -> Result<Self, Error> {
f9e3b110 26 Ok(Self {
2ab5acac 27 basedir: basedir.into(),
653b1ca1 28 router,
16b48b81 29 aliases: HashMap::new(),
02c7a755 30 env_type,
2ab5acac
DC
31 templates: RwLock::new(Handlebars::new()),
32 template_files: RwLock::new(HashMap::new()),
f9e3b110 33 })
16b48b81
DM
34 }
35
255f378a
DM
36 pub fn find_method(
37 &self,
38 components: &[&str],
39 method: Method,
40 uri_param: &mut HashMap<String, String>,
41 ) -> Option<&'static ApiMethod> {
16b48b81 42
01bf3b7b 43 self.router.find_method(components, method, uri_param)
16b48b81
DM
44 }
45
46 pub fn find_alias(&self, components: &[&str]) -> PathBuf {
47
48 let mut prefix = String::new();
49 let mut filename = self.basedir.clone();
50 let comp_len = components.len();
51 if comp_len >= 1 {
52 prefix.push_str(components[0]);
53 if let Some(subdir) = self.aliases.get(&prefix) {
54 filename.push(subdir);
55 for i in 1..comp_len { filename.push(components[i]) }
8adbdb0a
DM
56 } else {
57 for i in 0..comp_len { filename.push(components[i]) }
16b48b81
DM
58 }
59 }
60 filename
61 }
62
63 pub fn add_alias<S, P>(&mut self, alias: S, path: P)
64 where S: Into<String>,
65 P: Into<PathBuf>,
66 {
67 self.aliases.insert(alias.into(), path.into());
68 }
02c7a755
DM
69
70 pub fn env_type(&self) -> RpcEnvironmentType {
71 self.env_type
72 }
2ab5acac
DC
73
74 pub fn register_template<P>(&self, name: &str, path: P) -> Result<(), Error>
75 where
76 P: Into<PathBuf>
77 {
78 if self.template_files.read().unwrap().contains_key(name) {
79 bail!("template already registered");
80 }
81
82 let path: PathBuf = path.into();
83 let metadata = metadata(&path)?;
84 let mtime = metadata.modified()?;
85
86 self.templates.write().unwrap().register_template_file(name, &path)?;
87 self.template_files.write().unwrap().insert(name.to_string(), (mtime, path));
88
89 Ok(())
90 }
91
92 /// Checks if the template was modified since the last rendering
93 /// if yes, it loads a the new version of the template
94 pub fn render_template<T>(&self, name: &str, data: &T) -> Result<String, Error>
95 where
96 T: Serialize,
97 {
98 let path;
99 let mtime;
100 {
101 let template_files = self.template_files.read().unwrap();
102 let (old_mtime, old_path) = template_files.get(name).ok_or_else(|| format_err!("template not found"))?;
103
104 mtime = metadata(old_path)?.modified()?;
105 if mtime <= *old_mtime {
106 return self.templates.read().unwrap().render(name, data).map_err(|err| format_err!("{}", err));
107 }
108 path = old_path.to_path_buf();
109 }
110
111 {
112 let mut template_files = self.template_files.write().unwrap();
113 let mut templates = self.templates.write().unwrap();
114
115 templates.register_template_file(name, &path)?;
116 template_files.insert(name.to_string(), (mtime, path));
117
118 templates.render(name, data).map_err(|err| format_err!("{}", err))
119 }
120 }
16b48b81 121}