proxmox-backup/src/api3.rs

104 lines
2.6 KiB
Rust
Raw Normal View History

2018-11-03 09:42:48 +00:00
use failure::*;
use std::collections::HashMap;
use lazy_static::lazy_static;
use std::sync::{Arc, Mutex};
2018-11-03 09:42:48 +00:00
2018-11-15 16:07:10 +00:00
use crate::api::schema::*;
use crate::api::router::*;
2018-11-03 09:42:48 +00:00
use serde_json::{json, Value};
pub mod config;
2018-12-08 13:44:55 +00:00
mod version;
use crate::backup::datastore::*;
lazy_static!{
static ref datastore_map: Mutex<HashMap<String, Arc<DataStore>>> = Mutex::new(HashMap::new());
}
fn lookup_datastore(name: &str) -> Result<Arc<DataStore>, Error> {
let mut map = datastore_map.lock().unwrap();
if let Some(datastore) = map.get(name) {
return Ok(datastore.clone());
}
if let Ok(datastore) = DataStore::open(name) {
let datastore = Arc::new(datastore);
map.insert(name.to_string(), datastore.clone());
return Ok(datastore);
}
bail!("store not found");
}
2018-12-21 11:15:26 +00:00
// this is just a test for mutability/mutex handling - will remove later
fn test_sync_api_handler2(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
println!("This is a test {}", param);
let datastore = lookup_datastore("store1")?;
datastore.garbage_collection();
Ok(json!(null))
}
2018-11-14 16:32:17 +00:00
fn test_sync_api_handler(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
2018-11-03 09:42:48 +00:00
println!("This is a test {}", param);
// let force: Option<bool> = Some(false);
//if let Some(force) = param.force {
//}
let _force = param["force"].as_bool()
.ok_or_else(|| format_err!("missing parameter 'force'"))?;
if let Some(_force) = param["force"].as_bool() {
}
Ok(json!(null))
}
2018-11-15 10:46:13 +00:00
pub fn router() -> Router {
2018-11-03 09:42:48 +00:00
2018-12-05 13:03:15 +00:00
let route4 = Router::new()
2018-12-06 09:41:57 +00:00
.get(ApiMethod::new(
|param, _info| {
2018-11-15 16:47:59 +00:00
println!("This is a clousure handler: {}", param);
Ok(json!(null))
2018-12-06 09:41:57 +00:00
},
ObjectSchema::new("Another Endpoint."))
.returns(Schema::Null));
2018-11-15 12:28:15 +00:00
2018-12-05 13:03:15 +00:00
let nodeinfo = Router::new()
2018-12-06 09:23:45 +00:00
.get(ApiMethod::new(
test_sync_api_handler,
ObjectSchema::new("This is a simple test.")
.optional("force", BooleanSchema::new("Test for boolean options")))
)
.subdir("subdir3", route4);
2018-11-03 09:42:48 +00:00
2018-12-05 13:03:15 +00:00
let nodes = Router::new()
.match_all("node", nodeinfo);
2018-12-08 13:44:55 +00:00
let route = Router::new()
2018-12-06 09:41:57 +00:00
.get(ApiMethod::new(
2018-12-08 13:44:55 +00:00
|_,_| Ok(json!([
{"subdir": "config"},
{"subdir": "version"},
{"subdir": "nodes"}
])),
2018-12-06 09:41:57 +00:00
ObjectSchema::new("Directory index.")))
2018-12-08 13:44:55 +00:00
.subdir("config", config::router())
.subdir("version", version::router())
2018-12-06 09:23:45 +00:00
.subdir("nodes", nodes);
2018-11-15 12:28:15 +00:00
2018-11-03 14:10:21 +00:00
route
2018-11-03 09:42:48 +00:00
}