code cleanup
This commit is contained in:
parent
c578fcd9e2
commit
d3369f819d
|
@ -4,10 +4,24 @@ use crate::api::schema::*;
|
||||||
use serde_json::{Value};
|
use serde_json::{Value};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
type ApiHandlerFn = fn(Value, &ApiMethod) -> Result<Value, Error>;
|
||||||
|
|
||||||
pub struct ApiMethod {
|
pub struct ApiMethod {
|
||||||
pub parameters: ObjectSchema,
|
pub parameters: ObjectSchema,
|
||||||
pub returns: Schema,
|
pub returns: Schema,
|
||||||
pub handler: fn(Value, &ApiMethod) -> Result<Value, Error>,
|
pub handler: ApiHandlerFn,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ApiMethod {
|
||||||
|
|
||||||
|
pub fn new(handler: ApiHandlerFn, parameters: ObjectSchema) -> Self {
|
||||||
|
Self {
|
||||||
|
parameters,
|
||||||
|
handler,
|
||||||
|
returns: Schema::Null,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum SubRoute {
|
pub enum SubRoute {
|
||||||
|
@ -36,13 +50,30 @@ impl Router {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
pub fn subdirs(mut self, map: HashMap<String, Router>) -> Self {
|
pub fn subdirs(mut self, map: HashMap<String, Router>) -> Self {
|
||||||
self.subroute = SubRoute::Hash(map);
|
self.subroute = SubRoute::Hash(map);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn match_all<S>(mut self, param_name: S, router: Router) -> Self where S: Into<String> {
|
pub fn match_all<S: Into<String>>(mut self, param_name: S, router: Router) -> Self {
|
||||||
self.subroute = SubRoute::MatchAll { router: Box::new(router), param_name: param_name.into() };
|
if let SubRoute::None = self.subroute {
|
||||||
|
self.subroute = SubRoute::MatchAll { router: Box::new(router), param_name: param_name.into() };
|
||||||
|
} else {
|
||||||
|
panic!("unexpected subroute type");
|
||||||
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,17 +81,17 @@ impl Router {
|
||||||
self.get = Some(m);
|
self.get = Some(m);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn put(mut self, m: ApiMethod) -> Self {
|
pub fn put(mut self, m: ApiMethod) -> Self {
|
||||||
self.put = Some(m);
|
self.put = Some(m);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn post(mut self, m: ApiMethod) -> Self {
|
pub fn post(mut self, m: ApiMethod) -> Self {
|
||||||
self.post = Some(m);
|
self.post = Some(m);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete(mut self, m: ApiMethod) -> Self {
|
pub fn delete(mut self, m: ApiMethod) -> Self {
|
||||||
self.delete = Some(m);
|
self.delete = Some(m);
|
||||||
self
|
self
|
||||||
|
|
33
src/api3.rs
33
src/api3.rs
|
@ -54,27 +54,20 @@ pub fn router() -> Router {
|
||||||
|
|
||||||
|
|
||||||
let nodeinfo = Router::new()
|
let nodeinfo = Router::new()
|
||||||
.get(ApiMethod {
|
.get(ApiMethod::new(
|
||||||
handler: test_sync_api_handler,
|
test_sync_api_handler,
|
||||||
parameters: ObjectSchema::new("This is a simple test.")
|
ObjectSchema::new("This is a simple test.")
|
||||||
.optional("force", BooleanSchema::new("Test for boolean options")),
|
.optional("force", BooleanSchema::new("Test for boolean options")))
|
||||||
returns: Schema::Null,
|
)
|
||||||
})
|
.subdir("subdir3", route4);
|
||||||
.subdirs({
|
|
||||||
let mut map = HashMap::new();
|
|
||||||
map.insert("subdir3".into(), route4);
|
|
||||||
map
|
|
||||||
});
|
|
||||||
|
|
||||||
let nodes = Router::new()
|
let nodes = Router::new()
|
||||||
.match_all("node", nodeinfo);
|
.match_all("node", nodeinfo);
|
||||||
|
|
||||||
let version = Router::new()
|
let version = Router::new()
|
||||||
.get(ApiMethod {
|
.get(ApiMethod::new(
|
||||||
handler: get_version,
|
get_version,
|
||||||
parameters: ObjectSchema::new("Proxmox Backup Server API version."),
|
ObjectSchema::new("Proxmox Backup Server API version.")));
|
||||||
returns: Schema::Null,
|
|
||||||
});
|
|
||||||
|
|
||||||
let route = Router::new()
|
let route = Router::new()
|
||||||
.get(ApiMethod {
|
.get(ApiMethod {
|
||||||
|
@ -82,12 +75,8 @@ pub fn router() -> Router {
|
||||||
parameters: ObjectSchema::new("Directory index."),
|
parameters: ObjectSchema::new("Directory index."),
|
||||||
returns: Schema::Null,
|
returns: Schema::Null,
|
||||||
})
|
})
|
||||||
.subdirs({
|
.subdir("version", version)
|
||||||
let mut map = HashMap::new();
|
.subdir("nodes", nodes);
|
||||||
map.insert("version".into(), version);
|
|
||||||
map.insert("nodes".into(), nodes);
|
|
||||||
map
|
|
||||||
});
|
|
||||||
|
|
||||||
route
|
route
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue