proxmox-backup/src/api/router.rs

132 lines
3.3 KiB
Rust
Raw Normal View History

2018-11-15 16:07:55 +00:00
use failure::*;
use crate::api::schema::*;
use serde_json::{Value};
use std::collections::HashMap;
2018-12-06 09:41:57 +00:00
use std::sync::Arc;
2018-11-15 16:07:55 +00:00
2018-12-06 09:23:45 +00:00
type ApiHandlerFn = fn(Value, &ApiMethod) -> Result<Value, Error>;
2018-11-15 16:07:55 +00:00
pub struct ApiMethod {
pub parameters: ObjectSchema,
2018-12-06 09:41:57 +00:00
pub returns: Arc<Schema>,
2018-12-06 09:23:45 +00:00
pub handler: ApiHandlerFn,
}
impl ApiMethod {
pub fn new(handler: ApiHandlerFn, parameters: ObjectSchema) -> Self {
Self {
parameters,
handler,
2018-12-06 09:41:57 +00:00
returns: Arc::new(Schema::Null),
2018-12-06 09:23:45 +00:00
}
}
2018-12-06 09:41:57 +00:00
pub fn returns<S: Into<Arc<Schema>>>(mut self, schema: S) -> Self {
self.returns = schema.into();
self
}
2018-11-15 16:07:55 +00:00
}
pub enum SubRoute {
None,
Hash(HashMap<String, Router>),
MatchAll { router: Box<Router>, param_name: String },
}
pub struct Router {
pub get: Option<ApiMethod>,
pub put: Option<ApiMethod>,
pub post: Option<ApiMethod>,
pub delete: Option<ApiMethod>,
pub subroute: SubRoute,
}
impl Router {
pub fn new() -> Self {
Self {
get: None,
put: None,
post: None,
delete: None,
subroute: SubRoute::None
}
}
2018-12-06 09:23:45 +00:00
pub fn subdir<S: Into<String>>(mut self, subdir: S, router: Router) -> Self {
if let SubRoute::None = self.subroute {
self.subroute = SubRoute::Hash(HashMap::new());
}
match self.subroute {
SubRoute::Hash(ref mut map) => {
map.insert(subdir.into(), router);
}
_ => panic!("unexpected subroute type"),
}
self
}
2018-11-15 16:07:55 +00:00
pub fn subdirs(mut self, map: HashMap<String, Router>) -> Self {
self.subroute = SubRoute::Hash(map);
self
}
2018-12-06 09:23:45 +00:00
pub fn match_all<S: Into<String>>(mut self, param_name: S, router: Router) -> Self {
if let SubRoute::None = self.subroute {
self.subroute = SubRoute::MatchAll { router: Box::new(router), param_name: param_name.into() };
} else {
panic!("unexpected subroute type");
}
2018-11-15 16:07:55 +00:00
self
}
pub fn get(mut self, m: ApiMethod) -> Self {
self.get = Some(m);
self
}
2018-12-06 09:23:45 +00:00
pub fn put(mut self, m: ApiMethod) -> Self {
self.put = Some(m);
self
}
2018-12-06 09:23:45 +00:00
pub fn post(mut self, m: ApiMethod) -> Self {
self.post = Some(m);
self
}
2018-12-06 09:23:45 +00:00
pub fn delete(mut self, m: ApiMethod) -> Self {
self.delete = Some(m);
self
}
2018-11-15 16:07:55 +00:00
2018-11-16 08:15:33 +00:00
pub fn find_route(&self, components: &[&str], uri_param: &mut HashMap<String, String>) -> Option<&Router> {
2018-11-15 16:07:55 +00:00
if components.len() == 0 { return Some(self); };
let (dir, rest) = (components[0], &components[1..]);
match self.subroute {
SubRoute::None => {},
SubRoute::Hash(ref dirmap) => {
if let Some(ref router) = dirmap.get(dir) {
println!("FOUND SUBDIR {}", dir);
2018-11-16 08:15:33 +00:00
return router.find_route(rest, uri_param);
2018-11-15 16:07:55 +00:00
}
}
SubRoute::MatchAll { ref router, ref param_name } => {
println!("URI PARAM {} = {}", param_name, dir); // fixme: store somewhere
2018-11-16 08:15:33 +00:00
uri_param.insert(param_name.clone(), dir.into());
return router.find_route(rest, uri_param);
2018-11-15 16:07:55 +00:00
},
}
None
}
}