From a46a2e8847b9ab263e135215849444b5c767b378 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sat, 3 Feb 2024 17:09:18 -0800 Subject: [PATCH] add configfile stuff --- .env | 2 +- .gitignore | 1 + Cargo.lock | 10 ++++++++++ Cargo.toml | 1 + src/conf.rs | 21 +++++++++++++++------ src/db.rs | 8 +++++--- 6 files changed, 33 insertions(+), 10 deletions(-) diff --git a/.env b/.env index 07f6915..504abbe 100644 --- a/.env +++ b/.env @@ -1 +1 @@ -DATABASE_URL=sqlite://${HOME}/.what2watch.db +DATABASE_URL=sqlite://${HOME}/.local/share/what2watch/what2watch.db diff --git a/.gitignore b/.gitignore index 1d5ae1e..750844e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target /libjulid.so ww-style +*.db diff --git a/Cargo.lock b/Cargo.lock index cb9d87c..da92495 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -635,6 +635,15 @@ dependencies = [ "dirs-sys", ] +[[package]] +name = "dirs" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225" +dependencies = [ + "dirs-sys", +] + [[package]] name = "dirs-sys" version = "0.4.1" @@ -2877,6 +2886,7 @@ dependencies = [ "chrono", "clap", "confy", + "dirs", "http 1.0.0", "julid-rs", "justerror", diff --git a/Cargo.toml b/Cargo.toml index 25a123b..082241c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ axum-macros = "0.4" chrono = { version = "0.4", default-features = false, features = ["std", "clock"] } clap = { version = "4", features = ["derive", "env", "unicode", "suggestions", "usage"] } confy = "0.6" +dirs = "5" http = "1" julid-rs = "1" justerror = "1" diff --git a/src/conf.rs b/src/conf.rs index d77105c..1fa17bb 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -1,33 +1,42 @@ -use std::ffi::OsString; - use serde::{Deserialize, Serialize}; pub const APP_NAME: &str = "what2watch"; +const CONFIG_NAME: Option<&str> = Some("config"); #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] pub struct Config { pub base_url: String, - pub db_file: OsString, + pub db_file: String, } impl Default for Config { fn default() -> Self { + let mut datadir = dirs::data_dir().unwrap(); + datadir.push(APP_NAME); + datadir.push("what2watch.db"); Self { base_url: "http://localhost:3000".into(), - db_file: "what2watch.db".into(), + db_file: datadir.as_os_str().to_string_lossy().to_string(), } } } impl Config { pub fn get() -> Self { - let config: Config = confy::load(APP_NAME, None).unwrap_or_else(|e| { + let config: Config = confy::load(APP_NAME, CONFIG_NAME).unwrap_or_else(|e| { tracing::debug!("Could not load {APP_NAME} config, got error {e}"); Default::default() }); - confy::store(APP_NAME, None, config.clone()).unwrap_or_else(|e| { + confy::store(APP_NAME, CONFIG_NAME, config.clone()).unwrap_or_else(|e| { tracing::debug!("Could not store {APP_NAME} config, got error {e}"); }); + tracing::info!("Loading config from {}", cpath()); config } } + +fn cpath() -> String { + confy::get_configuration_file_path(APP_NAME, CONFIG_NAME) + .map(|p| p.as_os_str().to_str().unwrap().to_string()) + .expect("couldn't get the path to the configuration file") +} diff --git a/src/db.rs b/src/db.rs index 7f94e93..3e64d9f 100644 --- a/src/db.rs +++ b/src/db.rs @@ -15,9 +15,11 @@ pub fn get_db_pool() -> SqlitePool { std::env::var("DATABASE_FILE").unwrap_or_else(|_| { #[cfg(not(test))] { - let home = - std::env::var("HOME").expect("Could not determine $HOME for finding db file"); - format!("{home}/.what2watch.db") + let f = crate::conf::Config::get().db_file; + let p = std::path::Path::new(&f); + let p = p.parent().unwrap(); + std::fs::create_dir_all(p).expect("couldn't create data dir"); + f } #[cfg(test)] {