diff --git a/integrationtests/tests/tests.rs b/integrationtests/tests/tests.rs index cd1b0d9c..6807fce0 100644 --- a/integrationtests/tests/tests.rs +++ b/integrationtests/tests/tests.rs @@ -17,7 +17,7 @@ use tokio::time::{sleep_until, Instant}; #[test] #[cfg(feature = "integration-tests")] pub fn test_integration() -> anyhow::Result<()> { - use mokshamint::config::{LightningFeeConfig, ServerConfig}; + use mokshamint::config::{DatabaseConfig, LightningFeeConfig, ServerConfig}; let docker = clients::Cli::default(); let node = docker.run(Postgres::default()); @@ -41,13 +41,12 @@ pub fn test_integration() -> anyhow::Result<()> { host_port: "127.0.0.1:8686".parse().expect("invalid address"), ..Default::default() }) - .with_db( - format!( - "postgres://postgres:postgres@127.0.0.1:{}/moksha-mint", + .with_db(DatabaseConfig { + url: Some(format!( + "postgres://postgres:postgres@localhost:{}/moksha-mint", host_port - ) - .to_owned(), - ) + )), + }) .with_lightning(LightningType::Lnbits(LnbitsLightningSettings::new( "my_admin_key", "http://127.0.0.1:6100", diff --git a/moksha-mint/src/bin/moksha-mint.rs b/moksha-mint/src/bin/moksha-mint.rs index cd7c8594..f7f18139 100644 --- a/moksha-mint/src/bin/moksha-mint.rs +++ b/moksha-mint/src/bin/moksha-mint.rs @@ -1,5 +1,5 @@ use mokshamint::{ - config::{LightningFeeConfig, MintInfoConfig, ServerConfig}, + config::{DatabaseConfig, LightningFeeConfig, MintInfoConfig, ServerConfig}, lightning::{ AlbyLightningSettings, LightningType, LnbitsLightningSettings, LndLightningSettings, StrikeLightningSettings, @@ -24,6 +24,7 @@ pub async fn main() -> anyhow::Result<()> { }; } + // TODO move to config module let ln_backend = get_env("MINT_LIGHTNING_BACKEND"); let ln_type = match ln_backend.as_str() { "Lnbits" => { @@ -61,12 +62,13 @@ pub async fn main() -> anyhow::Result<()> { let fee_config = LightningFeeConfig::from_env(); let server_config = ServerConfig::from_env(); + let db_config = DatabaseConfig::from_env(); let mint = MintBuilder::new() .with_mint_info(mint_info_settings) .with_server(server_config) .with_private_key(get_env("MINT_PRIVATE_KEY")) - .with_db(get_env("MINT_DB_URL")) + .with_db(db_config) .with_lightning(ln_type) .with_fee(fee_config) .build() diff --git a/moksha-mint/src/config.rs b/moksha-mint/src/config.rs index 3ccc66be..b725a902 100644 --- a/moksha-mint/src/config.rs +++ b/moksha-mint/src/config.rs @@ -8,6 +8,7 @@ pub struct MintConfig { pub build: BuildConfig, pub lightning_fee: LightningFeeConfig, pub server: ServerConfig, + pub database: DatabaseConfig, } impl MintConfig { @@ -16,12 +17,27 @@ impl MintConfig { build: BuildConfig, lightning_fee: LightningFeeConfig, server: ServerConfig, + database: DatabaseConfig, ) -> Self { Self { info, build, lightning_fee, server, + database, + } + } +} + +#[derive(Deserialize, Serialize, Debug, Clone, Default)] +pub struct DatabaseConfig { + pub url: Option, +} + +impl DatabaseConfig { + pub fn from_env() -> Self { + DatabaseConfig { + url: env::var("MINT_DB_URL").ok(), } } } diff --git a/moksha-mint/src/database/postgres.rs b/moksha-mint/src/database/postgres.rs index 0c45dea2..fe864488 100644 --- a/moksha-mint/src/database/postgres.rs +++ b/moksha-mint/src/database/postgres.rs @@ -8,7 +8,7 @@ use moksha_core::{ use sqlx::postgres::PgPoolOptions; use uuid::Uuid; -use crate::{error::MokshaMintError, model::Invoice}; +use crate::{config::DatabaseConfig, error::MokshaMintError, model::Invoice}; use super::Database; @@ -17,9 +17,12 @@ pub struct PostgresDB { } impl PostgresDB { - pub async fn new(url: &str) -> Result { + pub async fn new(config: &DatabaseConfig) -> Result { Ok(PostgresDB { - pool: PgPoolOptions::new().max_connections(5).connect(url).await?, + pool: PgPoolOptions::new() + .max_connections(5) // FIXME make max connections configurable + .connect(config.url.as_ref().expect("Database-url not set")) + .await?, }) } diff --git a/moksha-mint/src/mint.rs b/moksha-mint/src/mint.rs index 40abf009..98e2af5a 100644 --- a/moksha-mint/src/mint.rs +++ b/moksha-mint/src/mint.rs @@ -8,7 +8,9 @@ use moksha_core::{ }; use crate::{ - config::{BuildConfig, LightningFeeConfig, MintConfig, MintInfoConfig, ServerConfig}, + config::{ + BuildConfig, DatabaseConfig, LightningFeeConfig, MintConfig, MintInfoConfig, ServerConfig, + }, database::Database, error::MokshaMintError, lightning::{AlbyLightning, Lightning, LightningType, LnbitsLightning, StrikeLightning}, @@ -199,7 +201,7 @@ impl Mint { pub struct MintBuilder { private_key: Option, lightning_type: Option, - db_url: Option, + db_config: Option, fee_config: Option, mint_info_settings: Option, server_config: Option, @@ -225,8 +227,8 @@ impl MintBuilder { self } - pub fn with_db(mut self, db_url: String) -> MintBuilder { - self.db_url = Some(db_url); + pub fn with_db(mut self, db_config: DatabaseConfig) -> MintBuilder { + self.db_config = Some(db_config); self } @@ -267,12 +269,9 @@ impl MintBuilder { None => panic!("Lightning backend not set"), }; - let db = Arc::new( - crate::database::postgres::PostgresDB::new( - self.db_url.expect("MINT_DB_URL not set").as_str(), - ) - .await?, - ); + let db_config = self.db_config.expect("Database config not set"); + + let db = Arc::new(crate::database::postgres::PostgresDB::new(&db_config).await?); db.migrate().await; Ok(Mint::new( @@ -287,6 +286,7 @@ impl MintBuilder { BuildConfig::from_env(), self.fee_config.expect("fee-config not set"), self.server_config.unwrap_or_default(), + db_config, ), )) }