diff --git a/proxmox-rest-server/Cargo.toml b/proxmox-rest-server/Cargo.toml index 24473927..1795dd26 100644 --- a/proxmox-rest-server/Cargo.toml +++ b/proxmox-rest-server/Cargo.toml @@ -16,7 +16,7 @@ tokio = { workspace = true, features = [ "rt-multi-thread", "signal", "process" [dependencies] anyhow.workspace = true futures.workspace = true -handlebars.workspace = true +handlebars = { workspace = true, optional = true } http.workspace = true hyper = { workspace = true, features = [ "full" ] } lazy_static.workspace = true @@ -42,3 +42,7 @@ proxmox-router.workspace = true proxmox-schema = { workspace = true, features = [ "api-macro", "upid-api-impl" ] } proxmox-time.workspace = true proxmox-sys = { workspace = true, features = [ "logrotate", "timer" ] } + +[features] +default = [] +templates = ["dep:handlebars"] diff --git a/proxmox-rest-server/src/api_config.rs b/proxmox-rest-server/src/api_config.rs index 9e75b8db..b75cebd0 100644 --- a/proxmox-rest-server/src/api_config.rs +++ b/proxmox-rest-server/src/api_config.rs @@ -1,17 +1,12 @@ use std::collections::HashMap; -use std::fs::metadata; use std::path::PathBuf; use std::pin::Pin; -use std::sync::{Arc, Mutex, RwLock}; -use std::time::SystemTime; +use std::sync::{Arc, Mutex}; -use anyhow::{bail, format_err, Error}; +use anyhow::{format_err, Error}; use hyper::http::request::Parts; use hyper::{Body, Method, Response}; -use handlebars::Handlebars; -use serde::Serialize; - use proxmox_router::{ApiMethod, Router, RpcEnvironmentType, UserInformation}; use proxmox_sys::fs::{create_path, CreateOptions}; @@ -23,11 +18,12 @@ pub struct ApiConfig { router: &'static Router, aliases: HashMap, env_type: RpcEnvironmentType, - templates: RwLock>, - template_files: RwLock>, request_log: Option>>, auth_log: Option>>, adapter: Pin>, + + #[cfg(feature = "templates")] + templates: templates::Templates, } impl ApiConfig { @@ -50,18 +46,19 @@ impl ApiConfig { router: &'static Router, env_type: RpcEnvironmentType, adapter: impl ServerAdapter + 'static, - ) -> Result { - Ok(Self { + ) -> Self { + Self { basedir: basedir.into(), router, aliases: HashMap::new(), env_type, - templates: RwLock::new(Handlebars::new()), - template_files: RwLock::new(HashMap::new()), request_log: None, auth_log: None, adapter: Box::pin(adapter), - }) + + #[cfg(feature = "templates")] + templates: Default::default(), + } } pub(crate) async fn get_index( @@ -132,67 +129,22 @@ impl ApiConfig { /// Register a [Handlebars] template file /// /// Those templates cane be use with [render_template](Self::render_template) to generate pages. + #[cfg(feature = "templates")] pub fn register_template

(&self, name: &str, path: P) -> Result<(), Error> where P: Into, { - if self.template_files.read().unwrap().contains_key(name) { - bail!("template already registered"); - } - - let path: PathBuf = path.into(); - let metadata = metadata(&path)?; - let mtime = metadata.modified()?; - - self.templates - .write() - .unwrap() - .register_template_file(name, &path)?; - self.template_files - .write() - .unwrap() - .insert(name.to_string(), (mtime, path)); - - Ok(()) + self.templates.register(name, path) } /// Checks if the template was modified since the last rendering /// if yes, it loads a the new version of the template + #[cfg(feature = "templates")] pub fn render_template(&self, name: &str, data: &T) -> Result where - T: Serialize, + T: serde::Serialize, { - let path; - let mtime; - { - let template_files = self.template_files.read().unwrap(); - let (old_mtime, old_path) = template_files - .get(name) - .ok_or_else(|| format_err!("template not found"))?; - - mtime = metadata(old_path)?.modified()?; - if mtime <= *old_mtime { - return self - .templates - .read() - .unwrap() - .render(name, data) - .map_err(|err| format_err!("{}", err)); - } - path = old_path.to_path_buf(); - } - - { - let mut template_files = self.template_files.write().unwrap(); - let mut templates = self.templates.write().unwrap(); - - templates.register_template_file(name, &path)?; - template_files.insert(name.to_string(), (mtime, path)); - - templates - .render(name, data) - .map_err(|err| format_err!("{}", err)) - } + self.templates.render(name, data) } /// Enable the access log feature @@ -282,3 +234,85 @@ impl ApiConfig { self.auth_log.as_ref() } } + +#[cfg(feature = "templates")] +mod templates { + use std::collections::HashMap; + use std::fs::metadata; + use std::path::PathBuf; + use std::sync::RwLock; + use std::time::SystemTime; + + use anyhow::{bail, format_err, Error}; + use handlebars::Handlebars; + use serde::Serialize; + + #[derive(Default)] + pub struct Templates { + templates: RwLock>, + template_files: RwLock>, + } + + impl Templates { + pub fn register

(&self, name: &str, path: P) -> Result<(), Error> + where + P: Into, + { + if self.template_files.read().unwrap().contains_key(name) { + bail!("template already registered"); + } + + let path: PathBuf = path.into(); + let metadata = metadata(&path)?; + let mtime = metadata.modified()?; + + self.templates + .write() + .unwrap() + .register_template_file(name, &path)?; + self.template_files + .write() + .unwrap() + .insert(name.to_string(), (mtime, path)); + + Ok(()) + } + + pub fn render(&self, name: &str, data: &T) -> Result + where + T: Serialize, + { + let path; + let mtime; + { + let template_files = self.template_files.read().unwrap(); + let (old_mtime, old_path) = template_files + .get(name) + .ok_or_else(|| format_err!("template not found"))?; + + mtime = metadata(old_path)?.modified()?; + if mtime <= *old_mtime { + return self + .templates + .read() + .unwrap() + .render(name, data) + .map_err(|err| format_err!("{}", err)); + } + path = old_path.to_path_buf(); + } + + { + let mut template_files = self.template_files.write().unwrap(); + let mut templates = self.templates.write().unwrap(); + + templates.register_template_file(name, &path)?; + template_files.insert(name.to_string(), (mtime, path)); + + templates + .render(name, data) + .map_err(|err| format_err!("{}", err)) + } + } + } +}