Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More config file options. #589

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions dim-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions dim-core/src/routes/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub struct GlobalSettings {
pub priv_key: Option<String>,
pub ssl_cert: Option<String>,

pub db_path: String,
pub logs_dir: String,
pub cache_dir: String,
pub metadata_dir: String,
pub quiet_boot: bool,
Expand Down Expand Up @@ -49,6 +51,8 @@ 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,
Expand Down
12 changes: 8 additions & 4 deletions dim-database/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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<String> = Default::default();
}

static __GLOBAL: OnceCell<DbConnection> = OnceCell::new();
Expand All @@ -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<T, E> where T is a new connection session or E is a connection
/// error.
pub async fn get_conn() -> sqlx::Result<crate::DbConnection> {
Expand Down Expand Up @@ -157,13 +161,13 @@ pub async fn get_conn_logged() -> sqlx::Result<DbConnection> {
async fn internal_get_conn() -> sqlx::Result<DbConnection> {
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),
Expand Down
4 changes: 3 additions & 1 deletion dim/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {
Expand All @@ -51,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()
Expand Down