From b7e3f7348cccaa63ac1989fe167070fac60c9225 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Tue, 2 Nov 2021 16:03:10 +0530 Subject: [PATCH] create tmp cache directory if one is not specified --- config/default.toml | 2 +- src/data.rs | 2 +- src/settings.rs | 20 ++++++++++++++++---- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/config/default.toml b/config/default.toml index 3fa3f0d..3b9b917 100644 --- a/config/default.toml +++ b/config/default.toml @@ -1,6 +1,6 @@ debug = true source_code = "https://github.com/realaravinth/libmedium" -cache = "/var/lib/libmedium" +#cache = "/var/lib/libmedium" [server] # The port at which you want authentication to listen to diff --git a/src/data.rs b/src/data.rs index 251caa6..56f38ac 100644 --- a/src/data.rs +++ b/src/data.rs @@ -44,7 +44,7 @@ pub type AppData = web::Data; impl Data { pub fn new() -> AppData { - let path = Path::new(&SETTINGS.cache).join("posts_cache"); + let path = Path::new(SETTINGS.cache.as_ref().unwrap()).join("posts_cache"); let cache = sled::open(path).unwrap(); let posts = cache.open_tree("posts").unwrap(); AppData::new(Self { diff --git a/src/settings.rs b/src/settings.rs index 6de0e11..865ebc1 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -42,7 +42,7 @@ impl Server { #[derive(Debug, Clone, Deserialize)] pub struct Settings { pub debug: bool, - pub cache: String, + pub cache: Option, pub server: Server, pub source_code: String, } @@ -82,14 +82,26 @@ impl Settings { Err(e) => warn!("couldn't interpret PORT: {}", e), } - let settings: Settings = s.try_into()?; + let mut settings: Settings = s.try_into()?; - let cache_path = Path::new(&settings.cache); + if settings.cache.is_none() { + let tmp = env::temp_dir().join("libmedium_cache_path"); + if !tmp.exists() { + fs::create_dir_all(&tmp).unwrap() + } + settings.cache = Some(tmp.to_str().unwrap().to_string()) + } + + let cache_path = settings.cache.as_ref().unwrap(); + let cache_path = Path::new(&cache_path); if !cache_path.exists() { fs::create_dir(&cache_path).unwrap(); } if !cache_path.is_dir() { - panic!("Cache path {} must be a directory", &settings.cache); + panic!( + "Cache path {} must be a directory", + &settings.cache.as_ref().unwrap() + ); } Ok(settings) }