Skip to content

Commit

Permalink
feat: add env-variable for db max connections
Browse files Browse the repository at this point in the history
  • Loading branch information
ngutech21 committed Mar 5, 2024
1 parent 7788f0e commit 75b0dd9
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ RUST_LOG=debug
MINT_APP_ENV=dev
# connection string for the postgres database
MINT_DB_URL=postgres://postgres:[email protected]/moksha-mint
# Set the maximum number of connections that the pool should maintain (default 5) (optional)
MINT_DB_MAX_CONNECTIONS=5
# the private key of the mint
MINT_PRIVATE_KEY=superprivatesecretkey

Expand Down
1 change: 1 addition & 0 deletions integrationtests/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub fn test_integration() -> anyhow::Result<()> {
"postgres://postgres:postgres@localhost:{}/postgres",
host_port
),
..Default::default()
};

let mint = MintBuilder::new()
Expand Down
14 changes: 13 additions & 1 deletion moksha-mint/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,22 @@ impl From<BtcOnchainConfig> for Nut15 {
}
}

#[derive(Debug, Clone, Default, Parser)]
#[derive(Debug, Clone, Parser)]
pub struct DatabaseConfig {
#[clap(long, env = "MINT_DB_URL")]
pub db_url: String,

#[clap(long, default_value_t = 5, env = "MINT_DB_MAX_CONNECTIONS")]
pub max_connections: u32,
}

impl Default for DatabaseConfig {
fn default() -> Self {
Self {
db_url: "".to_owned(),
max_connections: 5,
}
}
}

#[derive(Debug, Clone, Parser)]
Expand Down
2 changes: 1 addition & 1 deletion moksha-mint/src/database/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl PostgresDB {
pub async fn new(config: &DatabaseConfig) -> Result<Self, sqlx::Error> {
Ok(Self {
pool: PgPoolOptions::new()
.max_connections(5) // FIXME make max connections configurable
.max_connections(config.max_connections)
.connect(config.db_url.as_str())
.await?,
})
Expand Down
1 change: 1 addition & 0 deletions moksha-mint/src/mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ mod tests {
&format!("postgres://postgres:[email protected]:{}/postgres", port);
let db = PostgresDB::new(&DatabaseConfig {
db_url: connection_string.to_owned(),
..Default::default()
})
.await?;
db.migrate().await;
Expand Down
1 change: 1 addition & 0 deletions moksha-mint/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ mod tests {
&format!("postgres://postgres:[email protected]:{}/postgres", port);
let db = PostgresDB::new(&DatabaseConfig {
db_url: connection_string.to_owned(),
..Default::default()
})
.await?;
db.migrate().await;
Expand Down

0 comments on commit 75b0dd9

Please sign in to comment.