diff --git a/init_db.sh b/init_db.sh index 5e482020..bb2d2dcc 100755 --- a/init_db.sh +++ b/init_db.sh @@ -1,6 +1,6 @@ #!/bin/sh echo "Reading database URL from settings.toml..." -DATABASE_URL=$(awk -F'"' '/url *= */ {print $2}' settings.toml) +DATABASE_URL=$(awk -F'"' '/url *= */ {print $2}' settings.tpl.toml) export DATABASE_URL echo "Database URL is: $DATABASE_URL" if ls mostro.db* 1> /dev/null 2>&1; then diff --git a/src/lightning/invoice.rs b/src/lightning/invoice.rs index 3d9c5516..65192dda 100644 --- a/src/lightning/invoice.rs +++ b/src/lightning/invoice.rs @@ -56,11 +56,14 @@ pub fn is_valid_invoice( #[cfg(test)] mod tests { - use std::path::PathBuf; use std::env::set_var; + use std::path::PathBuf; use super::is_valid_invoice; - use crate::{error::MostroError, settings::{init_global_settings, Settings}}; + use crate::{ + error::MostroError, + settings::{init_global_settings, Settings}, + }; #[test] fn test_wrong_amount_invoice() { diff --git a/src/main.rs b/src/main.rs index 3457a7d9..556f2905 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,16 +16,16 @@ use nostr_sdk::prelude::*; use scheduler::start_scheduler; use settings::Settings; use settings::{init_default_dir, init_global_settings}; -use std::{env::args, path::PathBuf}; +use std::{env::args, path::PathBuf, sync::OnceLock}; use tokio::sync::Mutex; +static MOSTRO_CONFIG: OnceLock = OnceLock::new(); + #[macro_use] extern crate lazy_static; lazy_static! { static ref RATE_EVENT_LIST: Mutex> = Mutex::new(vec![]); - // Global var - static ref MOSTRO_CONFIG : std::sync::Mutex = std::sync::Mutex::new(Settings { database: Default::default(), nostr: Default::default(), mostro: Default::default(), lightning: Default::default() }); } #[tokio::main] diff --git a/src/settings.rs b/src/settings.rs index 41f0ed4f..b5fa5e08 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -52,7 +52,7 @@ impl TryFrom for Database { type Error = Error; fn try_from(_: Settings) -> Result { - Ok(MOSTRO_CONFIG.lock().unwrap().database.clone()) + Ok(MOSTRO_CONFIG.get().unwrap().database.clone()) } } @@ -71,7 +71,7 @@ impl TryFrom for Lightning { type Error = Error; fn try_from(_: Settings) -> Result { - Ok(MOSTRO_CONFIG.lock().unwrap().lightning.clone()) + Ok(MOSTRO_CONFIG.get().unwrap().lightning.clone()) } } @@ -85,7 +85,7 @@ impl TryFrom for Nostr { type Error = Error; fn try_from(_: Settings) -> Result { - Ok(MOSTRO_CONFIG.lock().unwrap().nostr.clone()) + Ok(MOSTRO_CONFIG.get().unwrap().nostr.clone()) } } @@ -103,7 +103,7 @@ impl TryFrom for Mostro { type Error = Error; fn try_from(_: Settings) -> Result { - Ok(MOSTRO_CONFIG.lock().unwrap().mostro.clone()) + Ok(MOSTRO_CONFIG.get().unwrap().mostro.clone()) } } @@ -115,8 +115,8 @@ pub struct Settings { pub lightning: Lightning, } -pub fn init_global_settings(setting: Settings) { - *MOSTRO_CONFIG.lock().unwrap() = setting +pub fn init_global_settings(s: Settings) { + MOSTRO_CONFIG.set(s).unwrap() } impl Settings { @@ -137,6 +137,10 @@ impl Settings { // Add in settings from the environment (with a prefix of APP) // Eg.. `APP_DEBUG=1 ./target/app` would set the `debug` key .add_source(Environment::with_prefix("app")) + .set_override( + "database.url", + format!("sqlite://{}", config_path.display()), + )? .build()?; // You can deserialize the entire configuration as @@ -144,19 +148,19 @@ impl Settings { } pub fn get_ln() -> Lightning { - MOSTRO_CONFIG.lock().unwrap().lightning.clone() + MOSTRO_CONFIG.get().unwrap().lightning.clone() } pub fn get_mostro() -> Mostro { - MOSTRO_CONFIG.lock().unwrap().mostro.clone() + MOSTRO_CONFIG.get().unwrap().mostro.clone() } pub fn get_db() -> Database { - MOSTRO_CONFIG.lock().unwrap().database.clone() + MOSTRO_CONFIG.get().unwrap().database.clone() } pub fn get_nostr() -> Nostr { - MOSTRO_CONFIG.lock().unwrap().nostr.clone() + MOSTRO_CONFIG.get().unwrap().nostr.clone() } } @@ -206,7 +210,7 @@ pub fn init_default_dir(config_path: Option<&String>) -> Result { "y" | "" => { fs::create_dir(settings_dir_default.clone())?; println!("You have created mostro default directory!"); - println!("Please, copy settings.tpl.toml file in {} folder then edit fields with right values (see README.md)", settings_dir_default.display()); + println!("Please, copy settings.tpl.toml and mostro.db too files in {} folder then edit settings file fields with right values (see README.md)", settings_dir_default.display()); process::exit(0); } "n" => {