proxmox-backup/src/api/config.rs

62 lines
1.7 KiB
Rust
Raw Normal View History

2018-11-15 16:07:10 +00:00
use crate::api::router::*;
2018-11-15 09:14:08 +00:00
use std::collections::HashMap;
use std::path::{PathBuf};
use hyper::Method;
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>,
}
impl ApiConfig {
2018-11-15 10:46:13 +00:00
pub fn new<B: Into<PathBuf>>(basedir: B, router: &'static Router) -> Self {
2018-11-15 09:14:08 +00:00
Self {
basedir: basedir.into(),
router: router,
aliases: HashMap::new(),
}
}
pub fn find_method(&self, components: &[&str], method: Method, uri_param: &mut HashMap<String, String>) -> &'static MethodDefinition {
2018-11-15 09:14:08 +00:00
2018-11-16 08:15:33 +00:00
if let Some(info) = self.router.find_route(components, uri_param) {
return match method {
2018-11-15 09:14:08 +00:00
Method::GET => &info.get,
Method::PUT => &info.put,
Method::POST => &info.post,
Method::DELETE => &info.delete,
_ => &MethodDefinition::None,
2018-11-15 09:14:08 +00:00
};
}
&MethodDefinition::None
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());
}
}