src/api_schema/router.rs: implement list_subdirs() helper

This commit is contained in:
Dietmar Maurer 2019-04-16 12:07:02 +02:00
parent 062d4916ff
commit 13f1cc17ea
9 changed files with 36 additions and 81 deletions

View File

@ -34,22 +34,13 @@ pub fn router() -> Router {
.match_all("node", node::router()); .match_all("node", node::router());
let route = Router::new() let route = Router::new()
.get(ApiMethod::new(
|| Ok(json!([
{"subdir": "access"},
{"subdir": "admin"},
{"subdir": "config"},
{"subdir": "nodes"},
{"subdir": "subscription"},
{"subdir": "version"},
])),
ObjectSchema::new("Directory index.")))
.subdir("access", access::router()) .subdir("access", access::router())
.subdir("admin", admin::router()) .subdir("admin", admin::router())
.subdir("config", config::router()) .subdir("config", config::router())
.subdir("nodes", nodes) .subdir("nodes", nodes)
.subdir("subscription", subscription::router()) .subdir("subscription", subscription::router())
.subdir("version", version::router()); .subdir("version", version::router())
.list_subdirs();
route route
} }

View File

@ -69,11 +69,6 @@ fn create_ticket(
pub fn router() -> Router { pub fn router() -> Router {
let route = Router::new() let route = Router::new()
.get(ApiMethod::new(
|| Ok(json!([
{"subdir": "ticket"}
])),
ObjectSchema::new("Directory index.")))
.subdir( .subdir(
"ticket", "ticket",
Router::new() Router::new()
@ -97,7 +92,8 @@ pub fn router() -> Router {
.required("CSRFPreventionToken", StringSchema::new("Cross Site Request Forgery Prevention Token.")) .required("CSRFPreventionToken", StringSchema::new("Cross Site Request Forgery Prevention Token."))
).protected(true) ).protected(true)
) )
); )
.list_subdirs();
route route
} }

View File

@ -7,12 +7,8 @@ pub mod datastore;
pub fn router() -> Router { pub fn router() -> Router {
let route = Router::new() let route = Router::new()
.get(ApiMethod::new( .subdir("datastore", datastore::router())
|| Ok(json!([ .list_subdirs();
{"subdir": "datastore"}
])),
ObjectSchema::new("Directory index.")))
.subdir("datastore", datastore::router());
route route
} }

View File

@ -387,19 +387,6 @@ pub fn router() -> Router {
); );
let datastore_info = Router::new() let datastore_info = Router::new()
.get(ApiMethod::new(
|| Ok(json!([
{"subdir": "backups" },
{"subdir": "pxar" },
{"subdir": "gc" },
{"subdir": "groups" },
{"subdir": "snapshots" },
//{"subdir": "status" },
{"subdir": "prune" },
])),
ObjectSchema::new("Directory index.")
.required("store", store_schema.clone()))
)
.subdir( .subdir(
"backups", "backups",
Router::new() Router::new()
@ -470,7 +457,9 @@ pub fn router() -> Router {
.subdir( .subdir(
"prune", "prune",
Router::new() Router::new()
.post(api_method_prune())); .post(api_method_prune())
)
.list_subdirs();

View File

@ -1,7 +1,7 @@
//use failure::*; //use failure::*;
//use std::collections::HashMap; //use std::collections::HashMap;
use crate::api_schema::*; //use crate::api_schema;
use crate::api_schema::router::*; use crate::api_schema::router::*;
use serde_json::{json}; use serde_json::{json};
@ -10,12 +10,8 @@ pub mod datastore;
pub fn router() -> Router { pub fn router() -> Router {
let route = Router::new() let route = Router::new()
.get(ApiMethod::new( .subdir("datastore", datastore::router())
|| Ok(json!([ .list_subdirs();
{"subdir": "datastore"},
])),
ObjectSchema::new("Directory index.")))
.subdir("datastore", datastore::router());
route route

View File

@ -34,21 +34,13 @@ lazy_static!{
pub fn router() -> Router { pub fn router() -> Router {
let route = Router::new() let route = Router::new()
.get(ApiMethod::new(
|| Ok(json!([
{"subdir": "dns"},
{"subdir": "network"},
{"subdir": "services"},
{"subdir": "syslog"},
{"subdir": "time"},
])),
ObjectSchema::new("Directory index.")))
.subdir("dns", dns::router()) .subdir("dns", dns::router())
.subdir("network", network::router()) .subdir("network", network::router())
.subdir("services", services::router()) .subdir("services", services::router())
.subdir("syslog", syslog::router()) .subdir("syslog", syslog::router())
.subdir("tasks", tasks::router()) .subdir("tasks", tasks::router())
.subdir("time", time::router()); .subdir("time", time::router())
.list_subdirs();
route route
} }

View File

@ -223,18 +223,6 @@ pub fn router() -> Router {
); );
let service_api = Router::new() let service_api = Router::new()
.get(ApiMethod::new(
|| {
let mut result = vec![];
for cmd in &["state", "start", "stop", "restart", "reload"] {
result.push(json!({"subdir": cmd }));
}
Ok(Value::from(result))
},
ObjectSchema::new("Directory index.")
.required("node", crate::api2::node::NODE_SCHEMA.clone())
.required("service", service_id_schema.clone()))
)
.subdir( .subdir(
"state", "state",
Router::new() Router::new()
@ -293,7 +281,7 @@ pub fn router() -> Router {
).protected(true) ).protected(true)
) )
) )
; .list_subdirs();
let route = Router::new() let route = Router::new()
.get( .get(

View File

@ -174,18 +174,6 @@ pub fn router() -> Router {
); );
let upid_api = Router::new() let upid_api = Router::new()
.get(ApiMethod::new(
|| {
let mut result = vec![];
for cmd in &["log", "status"] {
result.push(json!({"subdir": cmd }));
}
Ok(Value::from(result))
},
ObjectSchema::new("Directory index.")
.required("node", crate::api2::node::NODE_SCHEMA.clone())
.required("upid", upid_schema.clone()))
)
.delete(ApiMethod::new( .delete(ApiMethod::new(
stop_task, stop_task,
ObjectSchema::new("Try to stop a task.") ObjectSchema::new("Try to stop a task.")
@ -225,7 +213,8 @@ pub fn router() -> Router {
.required("node", crate::api2::node::NODE_SCHEMA.clone()) .required("node", crate::api2::node::NODE_SCHEMA.clone())
.required("upid", upid_schema.clone())) .required("upid", upid_schema.clone()))
) )
); )
.list_subdirs();
let route = Router::new() let route = Router::new()

View File

@ -1,7 +1,7 @@
use failure::*; use failure::*;
use crate::api_schema::*; use crate::api_schema::*;
use serde_json::{Value}; use serde_json::{json, Value};
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use std::fmt; use std::fmt;
@ -229,6 +229,24 @@ impl Router {
self self
} }
pub fn list_subdirs(self) -> Self {
match self.get {
MethodDefinition::None => {},
_ => panic!("cannot create directory index - method get already in use"),
}
match self.subroute {
SubRoute::Hash(ref map) => {
let index = json!(map.keys().map(|s| json!({ "subdira": s}))
.collect::<Vec<Value>>());
self.get(ApiMethod::new(
move || { Ok(index.clone()) },
ObjectSchema::new("Directory index.").additional_properties(true))
)
}
_ => panic!("cannot create directory index (no SubRoute::Hash)"),
}
}
pub fn get(mut self, m: ApiMethod) -> Self { pub fn get(mut self, m: ApiMethod) -> Self {
self.get = MethodDefinition::Simple(m); self.get = MethodDefinition::Simple(m);
self self