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