2018-11-15 09:14:08 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::path::{PathBuf};
|
2020-04-29 09:59:31 +00:00
|
|
|
use anyhow::Error;
|
2018-11-15 09:14:08 +00:00
|
|
|
|
|
|
|
use hyper::Method;
|
2020-04-29 09:59:31 +00:00
|
|
|
use handlebars::Handlebars;
|
2018-11-15 09:14:08 +00:00
|
|
|
|
2019-11-21 13:36:28 +00:00
|
|
|
use proxmox::api::{ApiMethod, Router, RpcEnvironmentType};
|
|
|
|
|
2018-11-15 09:14:08 +00:00
|
|
|
pub struct ApiConfig {
|
|
|
|
basedir: PathBuf,
|
2018-11-15 10:46:13 +00:00
|
|
|
router: &'static Router,
|
2018-11-15 09:14:08 +00:00
|
|
|
aliases: HashMap<String, PathBuf>,
|
2019-01-28 12:17:03 +00:00
|
|
|
env_type: RpcEnvironmentType,
|
2020-04-29 09:59:31 +00:00
|
|
|
pub templates: Handlebars<'static>,
|
2018-11-15 09:14:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ApiConfig {
|
|
|
|
|
2020-04-29 09:59:31 +00:00
|
|
|
pub fn new<B: Into<PathBuf>>(basedir: B, router: &'static Router, env_type: RpcEnvironmentType) -> Result<Self, Error> {
|
|
|
|
let mut templates = Handlebars::new();
|
|
|
|
let basedir = basedir.into();
|
|
|
|
templates.register_template_file("index", basedir.join("index.hbs"))?;
|
|
|
|
Ok(Self {
|
|
|
|
basedir,
|
2019-09-11 10:06:59 +00:00
|
|
|
router,
|
2018-11-15 09:14:08 +00:00
|
|
|
aliases: HashMap::new(),
|
2019-01-28 12:17:03 +00:00
|
|
|
env_type,
|
2020-04-29 09:59:31 +00:00
|
|
|
templates
|
|
|
|
})
|
2018-11-15 09:14:08 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 08:36:41 +00:00
|
|
|
pub fn find_method(
|
|
|
|
&self,
|
|
|
|
components: &[&str],
|
|
|
|
method: Method,
|
|
|
|
uri_param: &mut HashMap<String, String>,
|
|
|
|
) -> Option<&'static ApiMethod> {
|
2018-11-15 09:14:08 +00:00
|
|
|
|
2019-05-07 09:08:30 +00:00
|
|
|
self.router.find_method(components, method, uri_param)
|
2018-11-15 09:14:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_alias(&self, components: &[&str]) -> PathBuf {
|
|
|
|
|
|
|
|
let mut prefix = String::new();
|
|
|
|
let mut filename = self.basedir.clone();
|
|
|
|
let comp_len = components.len();
|
|
|
|
if comp_len >= 1 {
|
|
|
|
prefix.push_str(components[0]);
|
|
|
|
if let Some(subdir) = self.aliases.get(&prefix) {
|
|
|
|
filename.push(subdir);
|
|
|
|
for i in 1..comp_len { filename.push(components[i]) }
|
2018-12-01 14:21:25 +00:00
|
|
|
} else {
|
|
|
|
for i in 0..comp_len { filename.push(components[i]) }
|
2018-11-15 09:14:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
filename
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_alias<S, P>(&mut self, alias: S, path: P)
|
|
|
|
where S: Into<String>,
|
|
|
|
P: Into<PathBuf>,
|
|
|
|
{
|
|
|
|
self.aliases.insert(alias.into(), path.into());
|
|
|
|
}
|
2019-01-28 12:17:03 +00:00
|
|
|
|
|
|
|
pub fn env_type(&self) -> RpcEnvironmentType {
|
|
|
|
self.env_type
|
|
|
|
}
|
2018-11-15 09:14:08 +00:00
|
|
|
}
|