Skip to content

Commit

Permalink
chore: add db-config
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Dec 26, 2023
1 parent 3a07833 commit 91f9c20
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 22 deletions.
13 changes: 6 additions & 7 deletions integrationtests/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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",
Expand Down
6 changes: 4 additions & 2 deletions moksha-mint/src/bin/moksha-mint.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use mokshamint::{
config::{LightningFeeConfig, MintInfoConfig, ServerConfig},
config::{DatabaseConfig, LightningFeeConfig, MintInfoConfig, ServerConfig},
lightning::{
AlbyLightningSettings, LightningType, LnbitsLightningSettings, LndLightningSettings,
StrikeLightningSettings,
Expand All @@ -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" => {
Expand Down Expand Up @@ -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()
Expand Down
16 changes: 16 additions & 0 deletions moksha-mint/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub struct MintConfig {
pub build: BuildConfig,
pub lightning_fee: LightningFeeConfig,
pub server: ServerConfig,
pub database: DatabaseConfig,
}

impl MintConfig {
Expand All @@ -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<String>,
}

impl DatabaseConfig {
pub fn from_env() -> Self {
DatabaseConfig {
url: env::var("MINT_DB_URL").ok(),
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions moksha-mint/src/database/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -17,9 +17,12 @@ pub struct PostgresDB {
}

impl PostgresDB {
pub async fn new(url: &str) -> Result<PostgresDB, sqlx::Error> {
pub async fn new(config: &DatabaseConfig) -> Result<PostgresDB, sqlx::Error> {
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?,
})
}

Expand Down
20 changes: 10 additions & 10 deletions moksha-mint/src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -199,7 +201,7 @@ impl Mint {
pub struct MintBuilder {
private_key: Option<String>,
lightning_type: Option<LightningType>,
db_url: Option<String>,
db_config: Option<DatabaseConfig>,
fee_config: Option<LightningFeeConfig>,
mint_info_settings: Option<MintInfoConfig>,
server_config: Option<ServerConfig>,
Expand All @@ -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
}

Expand Down Expand Up @@ -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(
Expand All @@ -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,
),
))
}
Expand Down

0 comments on commit 91f9c20

Please sign in to comment.