diff --git a/core/src/obs/mod.rs b/core/src/obs/mod.rs index c70b048c..09587cb6 100644 --- a/core/src/obs/mod.rs +++ b/core/src/obs/mod.rs @@ -17,19 +17,21 @@ use std::fs; use std::sync::Arc; use url::Url; -static STORE: Lazy> = - Lazy::new(|| match std::env::var("SURREAL_OBJECT_STORE") { +fn initialize_store(env_var: &str, default_dir: &str) -> Arc { + match std::env::var(env_var) { Ok(url) => { - let url = Url::parse(&url).expect("Expected a valid url for SURREAL_OBJECT_STORE"); + let url = + Url::parse(&url).unwrap_or_else(|_| panic!("Expected a valid url for {}", env_var)); let (store, _) = - parse_url(&url).expect("Expected a valid url for SURREAL_OBJECT_STORE"); + parse_url(&url).unwrap_or_else(|_| panic!("Expected a valid url for {}", env_var)); Arc::new(store) } Err(_) => { - let path = env::current_dir().unwrap().join("store"); + let path = env::current_dir().unwrap().join(default_dir); if !path.exists() || !path.is_dir() { - fs::create_dir_all(&path) - .expect("Unable to create directory structure for SURREAL_OBJECT_STORE"); + fs::create_dir_all(&path).unwrap_or_else(|_| { + panic!("Unable to create directory structure for {}", env_var) + }); } #[cfg(not(target_arch = "wasm32"))] { @@ -41,33 +43,14 @@ static STORE: Lazy> = Arc::new(InMemory::new()) } } - }); + } +} + +static STORE: Lazy> = + Lazy::new(|| initialize_store("SURREAL_OBJECT_STORE", "store")); static CACHE: Lazy> = - Lazy::new(|| match std::env::var("SURREAL_OBJECT_CACHE") { - Ok(url) => { - let url = Url::parse(&url).expect("Expected a valid url for SURREAL_OBJECT_CACHE"); - let (store, _) = - parse_url(&url).expect("Expected a valid url for SURREAL_OBJECT_CACHE"); - Arc::new(store) - } - Err(_) => { - let path = env::current_dir().unwrap().join("cache"); - if !path.exists() || !path.is_dir() { - fs::create_dir_all(&path) - .expect("Unable to create directory structure for SURREAL_OBJECT_CACHE"); - } - #[cfg(not(target_arch = "wasm32"))] - { - // As long as the provided path is correct, the following should never panic - Arc::new(LocalFileSystem::new_with_prefix(path).unwrap()) - } - #[cfg(target_arch = "wasm32")] - { - Arc::new(InMemory::new()) - } - } - }); + Lazy::new(|| initialize_store("SURREAL_CACHE_STORE", "cache")); /// Streams the file from the local system or memory object storage. pub async fn stream(