diff --git a/config/src/config.rs b/config/src/config.rs index 795c382fe..8235d0030 100644 --- a/config/src/config.rs +++ b/config/src/config.rs @@ -22,6 +22,8 @@ use url::Url; use crate::NonZeroGRT; +const SHARED_PREFIX: &str = "INDEXER_"; + #[derive(Debug, Deserialize)] #[cfg_attr(test, derive(PartialEq))] pub struct Config { @@ -81,11 +83,12 @@ impl Config { config_content = Self::substitute_env_vars(config_content)?; figment_config = figment_config.merge(Toml::string(&config_content)); } - let config: ConfigWrapper = figment_config .merge(Env::prefixed(prefix.get_prefix()).split("__")) + .merge(Env::prefixed(SHARED_PREFIX).split("__")) .extract() .map_err(|e| e.to_string())?; + config.0.validate()?; Ok(config.0) } @@ -674,4 +677,47 @@ mod tests { "postgres://postgres@postgres/postgres" ); } + + // Test that we can fill in mandatory config fields missing from the config file with + // environment variables + #[sealed_test(files = ["minimal-config-example.toml"])] + fn test_fill_in_missing_with_shared_env() { + let mut minimal_config: toml::Value = toml::from_str( + fs::read_to_string("minimal-config-example.toml") + .unwrap() + .as_str(), + ) + .unwrap(); + // Remove the database.postgres_url field from minimal config + minimal_config + .get_mut("database") + .unwrap() + .as_table_mut() + .unwrap() + .remove("postgres_url"); + + // Save the modified minimal config to a named temporary file using tempfile + let temp_minimal_config_path = tempfile::NamedTempFile::new().unwrap(); + fs::write( + temp_minimal_config_path.path(), + toml::to_string(&minimal_config).unwrap(), + ) + .unwrap(); + + // No need to parse since from another test we know parsing at this point it will fail + + let test_value = "postgres://postgres@postgres:5432/postgres"; + env::set_var("INDEXER_DATABASE__POSTGRES_URL", test_value); + + let config = Config::parse( + ConfigPrefix::Service, + Some(PathBuf::from(temp_minimal_config_path.path())).as_ref(), + ) + .unwrap(); + + assert_eq!( + config.database.get_formated_postgres_url().as_str(), + test_value + ); + } }