Skip to content

Commit

Permalink
De-duplicate database_url between ENV and config.ymls
Browse files Browse the repository at this point in the history
Fixes #190

Carp will now make the config's database_url optional, and if it is not
present, will try and default to the DATABASE_URL env variable which is
set dynamically.

TODO: docs update?
  • Loading branch information
rooooooooob committed Jul 14, 2024
1 parent a837160 commit 0997d28
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions indexer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,21 @@ pub struct Cli {
#[serde(tag = "type", rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
pub enum DbConfig {
Postgres { database_url: String },
Postgres {
#[serde(default = "get_env_db_url")]
database_url: String
},
}

#[derive(Debug, Clone, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
pub enum SinkConfig {
Cardano { db: DbConfig, network: String },
Cardano {
db: DbConfig,
#[serde(default = "get_env_network")]
network: String,
},
}

pub enum Network {}
Expand All @@ -76,6 +83,16 @@ pub struct Config {
start_block: Option<String>,
}

fn get_env_db_url() -> String {
std::env::var("DATABASE_URL")
.expect("env DATABASE_URL not found and config did not specify sink.db.database_url")
}

fn get_env_network() -> String {
std::env::var("NETWORK")
.expect("env NETWORK not found and config did not specify sink.network")
}

#[allow(unreachable_patterns)]
#[tokio::main]
async fn main() -> anyhow::Result<()> {
Expand Down

0 comments on commit 0997d28

Please sign in to comment.