Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
);
}
}
Loading