From 2352f76ebafaa91a835bde1113fff845e03771bc Mon Sep 17 00:00:00 2001 From: Andrew 2E128 Date: Sun, 3 Dec 2023 02:48:29 +0200 Subject: [PATCH 1/2] Added db_path config option. --- dim-core/src/routes/settings.rs | 2 ++ dim-database/src/lib.rs | 12 ++++++++---- dim/src/main.rs | 2 ++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/dim-core/src/routes/settings.rs b/dim-core/src/routes/settings.rs index f577eaf6c..b4f72fbb9 100644 --- a/dim-core/src/routes/settings.rs +++ b/dim-core/src/routes/settings.rs @@ -21,6 +21,7 @@ pub struct GlobalSettings { pub priv_key: Option, pub ssl_cert: Option, + pub db_path: String, pub cache_dir: String, pub metadata_dir: String, pub quiet_boot: bool, @@ -49,6 +50,7 @@ impl Default for GlobalSettings { } } }, + db_path: ffpath("config/dim.db"), metadata_dir: ffpath("config/metadata"), quiet_boot: false, disable_auth: false, diff --git a/dim-database/src/lib.rs b/dim-database/src/lib.rs index de99a5e49..ddda76e53 100644 --- a/dim-database/src/lib.rs +++ b/dim-database/src/lib.rs @@ -1,11 +1,10 @@ // FIXME: We have a shim in dim/utils but we cant depend on dim because itd be a circular dep. #![deny(warnings)] -use crate::utils::ffpath; - use std::str::FromStr; use std::sync::atomic::AtomicBool; use std::sync::atomic::Ordering; +use std::sync::Mutex; use sqlx::ConnectOptions; use tracing::{info, instrument}; @@ -43,6 +42,7 @@ pub type Transaction<'tx> = sqlx::Transaction<'tx, sqlx::Sqlite>; lazy_static::lazy_static! { static ref MIGRATIONS_FLAG: AtomicBool = AtomicBool::new(false); + static ref DB_PATH: Mutex = Default::default(); } static __GLOBAL: OnceCell = OnceCell::new(); @@ -57,6 +57,10 @@ async fn run_migrations(conn: &crate::DbConnection) -> Result<(), sqlx::migrate: MIGRATOR.run(&mut *lock).await } +pub fn set_db_path(db_path: String){ + *DB_PATH.lock().unwrap() = db_path; +} + /// Function which returns a Result where T is a new connection session or E is a connection /// error. pub async fn get_conn() -> sqlx::Result { @@ -157,13 +161,13 @@ pub async fn get_conn_logged() -> sqlx::Result { async fn internal_get_conn() -> sqlx::Result { let rw_only = sqlx::sqlite::SqliteConnectOptions::new() .create_if_missing(true) - .filename(ffpath("config/dim.db")) + .filename(DB_PATH.lock().unwrap().as_str()) .connect() .await?; let rd_only = sqlx::pool::PoolOptions::new() .connect_with( - sqlx::sqlite::SqliteConnectOptions::from_str(ffpath("config/dim.db"))? + sqlx::sqlite::SqliteConnectOptions::from_str(DB_PATH.lock().unwrap().as_str())? .read_only(true) .synchronous(sqlx::sqlite::SqliteSynchronous::Normal) .create_if_missing(true), diff --git a/dim/src/main.rs b/dim/src/main.rs index 867d64dee..0a6b14d98 100644 --- a/dim/src/main.rs +++ b/dim/src/main.rs @@ -33,6 +33,8 @@ fn main() { // never panics because we set a default value to metadata_dir let _ = std::fs::create_dir_all(global_settings.metadata_dir.clone()); + dim_database::set_db_path(global_settings.db_path.clone()); + // set our jwt secret key let settings_clone = global_settings.clone(); let secret_key = global_settings.secret_key.unwrap_or_else(move || { From 8612c7eb5891de1db5c521d15899a75006bc8e54 Mon Sep 17 00:00:00 2001 From: Andrew 2E128 Date: Sun, 3 Dec 2023 21:43:26 +0200 Subject: [PATCH 2/2] Added logs_dir config option. --- dim-core/src/lib.rs | 6 +++--- dim-core/src/routes/settings.rs | 2 ++ dim/src/main.rs | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/dim-core/src/lib.rs b/dim-core/src/lib.rs index 3e70ff81a..e06a92d30 100644 --- a/dim-core/src/lib.rs +++ b/dim-core/src/lib.rs @@ -58,14 +58,14 @@ pub use routes::settings::set_global_settings; pub use routes::settings::GlobalSettings; /// Function builds a logger drain that drains to a json file located in logs/ and also to stdout. -pub fn setup_logging(_debug: bool) { - let _ = create_dir_all("logs"); +pub fn setup_logging(logs_dir: &String,_debug: bool) { + let _ = create_dir_all(&logs_dir); if std::env::var("RUST_LOG").is_err() { std::env::set_var("RUST_LOG", "info,tower_http=trace"); } - let log_appender = tracing_appender::rolling::daily("./logs", "dim-log.log"); + let log_appender = tracing_appender::rolling::daily(&logs_dir, "dim-log.log"); let (non_blocking_file, _guard) = tracing_appender::non_blocking(log_appender); let subscriber = tracing_subscriber::registry() diff --git a/dim-core/src/routes/settings.rs b/dim-core/src/routes/settings.rs index b4f72fbb9..03a19d8e8 100644 --- a/dim-core/src/routes/settings.rs +++ b/dim-core/src/routes/settings.rs @@ -22,6 +22,7 @@ pub struct GlobalSettings { pub ssl_cert: Option, pub db_path: String, + pub logs_dir: String, pub cache_dir: String, pub metadata_dir: String, pub quiet_boot: bool, @@ -51,6 +52,7 @@ impl Default for GlobalSettings { } }, db_path: ffpath("config/dim.db"), + logs_dir: "./logs".to_string(), metadata_dir: ffpath("config/metadata"), quiet_boot: false, disable_auth: false, diff --git a/dim/src/main.rs b/dim/src/main.rs index 0a6b14d98..1745a94e4 100644 --- a/dim/src/main.rs +++ b/dim/src/main.rs @@ -53,7 +53,7 @@ fn main() { .set(global_settings.metadata_dir.clone()) .expect("Failed to set METADATA_PATH"); - dim::setup_logging(global_settings.verbose); + dim::setup_logging(&global_settings.logs_dir, global_settings.verbose); { let failed = streaming::ffcheck()