From 0997d2857480636ab4e3e68a025cf67f03c28c07 Mon Sep 17 00:00:00 2001 From: rooooooooob Date: Sun, 14 Jul 2024 10:18:25 -0700 Subject: [PATCH] De-duplicate database_url between ENV and config.ymls 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? --- indexer/src/main.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/indexer/src/main.rs b/indexer/src/main.rs index 745d1233..1785c4d3 100644 --- a/indexer/src/main.rs +++ b/indexer/src/main.rs @@ -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 {} @@ -76,6 +83,16 @@ pub struct Config { start_block: Option, } +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<()> {