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

Some improvements on config file and db file #104

Merged
merged 1 commit into from
Aug 28, 2023
Merged
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
2 changes: 1 addition & 1 deletion init_db.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 5 additions & 2 deletions src/lightning/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Settings> = OnceLock::new();

#[macro_use]
extern crate lazy_static;

lazy_static! {
static ref RATE_EVENT_LIST: Mutex<Vec<Event>> = Mutex::new(vec![]);
// Global var
static ref MOSTRO_CONFIG : std::sync::Mutex<Settings> = std::sync::Mutex::new(Settings { database: Default::default(), nostr: Default::default(), mostro: Default::default(), lightning: Default::default() });
}

#[tokio::main]
Expand Down
26 changes: 15 additions & 11 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl TryFrom<Settings> for Database {
type Error = Error;

fn try_from(_: Settings) -> Result<Self, Error> {
Ok(MOSTRO_CONFIG.lock().unwrap().database.clone())
Ok(MOSTRO_CONFIG.get().unwrap().database.clone())
}
}

Expand All @@ -71,7 +71,7 @@ impl TryFrom<Settings> for Lightning {
type Error = Error;

fn try_from(_: Settings) -> Result<Self, Error> {
Ok(MOSTRO_CONFIG.lock().unwrap().lightning.clone())
Ok(MOSTRO_CONFIG.get().unwrap().lightning.clone())
}
}

Expand All @@ -85,7 +85,7 @@ impl TryFrom<Settings> for Nostr {
type Error = Error;

fn try_from(_: Settings) -> Result<Self, Error> {
Ok(MOSTRO_CONFIG.lock().unwrap().nostr.clone())
Ok(MOSTRO_CONFIG.get().unwrap().nostr.clone())
}
}

Expand All @@ -103,7 +103,7 @@ impl TryFrom<Settings> for Mostro {
type Error = Error;

fn try_from(_: Settings) -> Result<Self, Error> {
Ok(MOSTRO_CONFIG.lock().unwrap().mostro.clone())
Ok(MOSTRO_CONFIG.get().unwrap().mostro.clone())
}
}

Expand All @@ -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 {
Expand All @@ -137,26 +137,30 @@ 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
s.try_deserialize()
}

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()
}
}

Expand Down Expand Up @@ -206,7 +210,7 @@ pub fn init_default_dir(config_path: Option<&String>) -> Result<PathBuf> {
"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" => {
Expand Down