From c2d9b3914a75d34ecf4b74ed004803e88025d765 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 20 Dec 2018 14:03:42 +0100 Subject: [PATCH] add test code to access static global state --- src/api3.rs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/api3.rs b/src/api3.rs index 6753615b..2fb3f200 100644 --- a/src/api3.rs +++ b/src/api3.rs @@ -1,6 +1,8 @@ use failure::*; -//use std::collections::HashMap; +use std::collections::HashMap; +use lazy_static::lazy_static; +use std::sync::{Arc, Mutex}; use crate::api::schema::*; use crate::api::router::*; @@ -9,6 +11,29 @@ use serde_json::{json, Value}; pub mod config; mod version; +use crate::backup::datastore::*; + +lazy_static!{ + static ref datastore_map: Mutex>> = Mutex::new(HashMap::new()); +} + +fn lookup_datastore(name: &str) -> Result, 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"); +} + fn test_sync_api_handler(param: Value, _info: &ApiMethod) -> Result { println!("This is a test {}", param); @@ -23,7 +48,6 @@ fn test_sync_api_handler(param: Value, _info: &ApiMethod) -> Result