]> git.proxmox.com Git - proxmox-backup.git/blob - proxmox-rest-server/src/api_config.rs
a319e20405d5ab566675f87faa1820dfef6da96d
[proxmox-backup.git] / proxmox-rest-server / src / api_config.rs
1 use std::collections::HashMap;
2 use std::path::PathBuf;
3 use std::time::SystemTime;
4 use std::fs::metadata;
5 use std::sync::{Arc, Mutex, RwLock};
6
7 use anyhow::{bail, Error, format_err};
8 use hyper::Method;
9 use handlebars::Handlebars;
10 use serde::Serialize;
11
12 use proxmox::api::{ApiMethod, Router, RpcEnvironmentType};
13 use proxmox::tools::fs::{create_path, CreateOptions};
14
15 use crate::{ApiAuth, FileLogger, FileLogOptions, CommandoSocket};
16
17 pub struct ApiConfig {
18 basedir: PathBuf,
19 router: &'static Router,
20 aliases: HashMap<String, PathBuf>,
21 env_type: RpcEnvironmentType,
22 templates: RwLock<Handlebars<'static>>,
23 template_files: RwLock<HashMap<String, (SystemTime, PathBuf)>>,
24 request_log: Option<Arc<Mutex<FileLogger>>>,
25 pub api_auth: Arc<dyn ApiAuth + Send + Sync>,
26 }
27
28 impl ApiConfig {
29 pub fn new<B: Into<PathBuf>>(
30 basedir: B,
31 router: &'static Router,
32 env_type: RpcEnvironmentType,
33 api_auth: Arc<dyn ApiAuth + Send + Sync>,
34 ) -> Result<Self, Error> {
35 Ok(Self {
36 basedir: basedir.into(),
37 router,
38 aliases: HashMap::new(),
39 env_type,
40 templates: RwLock::new(Handlebars::new()),
41 template_files: RwLock::new(HashMap::new()),
42 request_log: None,
43 api_auth,
44 })
45 }
46
47 pub fn find_method(
48 &self,
49 components: &[&str],
50 method: Method,
51 uri_param: &mut HashMap<String, String>,
52 ) -> Option<&'static ApiMethod> {
53
54 self.router.find_method(components, method, uri_param)
55 }
56
57 pub fn find_alias(&self, components: &[&str]) -> PathBuf {
58
59 let mut prefix = String::new();
60 let mut filename = self.basedir.clone();
61 let comp_len = components.len();
62 if comp_len >= 1 {
63 prefix.push_str(components[0]);
64 if let Some(subdir) = self.aliases.get(&prefix) {
65 filename.push(subdir);
66 components.iter().skip(1).for_each(|comp| filename.push(comp));
67 } else {
68 components.iter().for_each(|comp| filename.push(comp));
69 }
70 }
71 filename
72 }
73
74 pub fn add_alias<S, P>(&mut self, alias: S, path: P)
75 where S: Into<String>,
76 P: Into<PathBuf>,
77 {
78 self.aliases.insert(alias.into(), path.into());
79 }
80
81 pub fn env_type(&self) -> RpcEnvironmentType {
82 self.env_type
83 }
84
85 pub fn register_template<P>(&self, name: &str, path: P) -> Result<(), Error>
86 where
87 P: Into<PathBuf>
88 {
89 if self.template_files.read().unwrap().contains_key(name) {
90 bail!("template already registered");
91 }
92
93 let path: PathBuf = path.into();
94 let metadata = metadata(&path)?;
95 let mtime = metadata.modified()?;
96
97 self.templates.write().unwrap().register_template_file(name, &path)?;
98 self.template_files.write().unwrap().insert(name.to_string(), (mtime, path));
99
100 Ok(())
101 }
102
103 /// Checks if the template was modified since the last rendering
104 /// if yes, it loads a the new version of the template
105 pub fn render_template<T>(&self, name: &str, data: &T) -> Result<String, Error>
106 where
107 T: Serialize,
108 {
109 let path;
110 let mtime;
111 {
112 let template_files = self.template_files.read().unwrap();
113 let (old_mtime, old_path) = template_files.get(name).ok_or_else(|| format_err!("template not found"))?;
114
115 mtime = metadata(old_path)?.modified()?;
116 if mtime <= *old_mtime {
117 return self.templates.read().unwrap().render(name, data).map_err(|err| format_err!("{}", err));
118 }
119 path = old_path.to_path_buf();
120 }
121
122 {
123 let mut template_files = self.template_files.write().unwrap();
124 let mut templates = self.templates.write().unwrap();
125
126 templates.register_template_file(name, &path)?;
127 template_files.insert(name.to_string(), (mtime, path));
128
129 templates.render(name, data).map_err(|err| format_err!("{}", err))
130 }
131 }
132
133 pub fn enable_file_log<P>(
134 &mut self,
135 path: P,
136 dir_opts: Option<CreateOptions>,
137 file_opts: Option<CreateOptions>,
138 commando_sock: &mut CommandoSocket,
139 ) -> Result<(), Error>
140 where
141 P: Into<PathBuf>
142 {
143 let path: PathBuf = path.into();
144 if let Some(base) = path.parent() {
145 if !base.exists() {
146 create_path(base, None, dir_opts).map_err(|err| format_err!("{}", err))?;
147 }
148 }
149
150 let logger_options = FileLogOptions {
151 append: true,
152 file_opts: file_opts.unwrap_or(CreateOptions::default()),
153 ..Default::default()
154 };
155 let request_log = Arc::new(Mutex::new(FileLogger::new(&path, logger_options)?));
156 self.request_log = Some(Arc::clone(&request_log));
157
158 commando_sock.register_command("api-access-log-reopen".into(), move |_args| {
159 println!("re-opening log file");
160 request_log.lock().unwrap().reopen()?;
161 Ok(serde_json::Value::Null)
162 })?;
163
164 Ok(())
165 }
166
167 pub fn get_file_log(&self) -> Option<&Arc<Mutex<FileLogger>>> {
168 self.request_log.as_ref()
169 }
170 }