create tmp cache directory if one is not specified

This commit is contained in:
Aravinth Manivannan 2021-11-02 16:03:10 +05:30
parent 96a08fcb3f
commit b7e3f7348c
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
3 changed files with 18 additions and 6 deletions

View file

@ -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

View file

@ -44,7 +44,7 @@ pub type AppData = web::Data<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 {

View file

@ -42,7 +42,7 @@ impl Server {
#[derive(Debug, Clone, Deserialize)]
pub struct Settings {
pub debug: bool,
pub cache: String,
pub cache: Option<String>,
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)
}