2 //use std::collections::HashMap;
4 use crate::api_schema
::*;
5 use crate::api_schema
::router
::*;
7 use serde_json
::{json, Value}
;
8 use std
::path
::PathBuf
;
10 use crate::config
::datastore
;
12 pub const GET
: ApiMethod
= ApiMethod
::new(
13 &ApiHandler
::Sync(&get_datastore_list
),
14 &ObjectSchema
::new("Directory index.", &[])
17 fn get_datastore_list(
20 _rpcenv
: &mut dyn RpcEnvironment
,
21 ) -> Result
<Value
, Error
> {
23 let config
= datastore
::config()?
;
25 Ok(config
.convert_to_array("name"))
28 pub const POST
: ApiMethod
= ApiMethod
::new(
29 &ApiHandler
::Sync(&create_datastore
),
31 "Create new datastore.",
33 ("name", false, &StringSchema
::new("Datastore name.").schema()),
34 ("path", false, &StringSchema
::new("Directory path (must exist).").schema()),
42 _rpcenv
: &mut dyn RpcEnvironment
,
43 ) -> Result
<Value
, Error
> {
47 let mut config
= datastore
::config()?
;
49 let name
= param
["name"].as_str().unwrap();
51 if let Some(_
) = config
.sections
.get(name
) {
52 bail
!("datastore '{}' already exists.", name
);
55 let path
: PathBuf
= param
["path"].as_str().unwrap().into();
56 let _store
= ChunkStore
::create(name
, path
)?
;
58 let datastore
= json
!({
62 config
.set_data(name
, "datastore", datastore
);
64 datastore
::save_config(&config
)?
;
69 pub const DELETE
: ApiMethod
= ApiMethod
::new(
70 &ApiHandler
::Sync(&delete_datastore
),
72 "Remove a datastore configuration.",
74 ("name", false, &StringSchema
::new("Datastore name.").schema()),
82 _rpcenv
: &mut dyn RpcEnvironment
,
83 ) -> Result
<Value
, Error
> {
84 println
!("This is a test {}", param
);
87 // fixme: check digest ?
89 let mut config
= datastore
::config()?
;
91 let name
= param
["name"].as_str().unwrap();
93 match config
.sections
.get(name
) {
94 Some(_
) => { config.sections.remove(name); }
,
95 None
=> bail
!("datastore '{}' does not exist.", name
),
98 datastore
::save_config(&config
)?
;
103 pub const ROUTER
: Router
= Router
::new()