Skip to content

Commit 263e39b

Browse files
committed
Always override config defaults with env vars.
Fixes a regression introduced in f7b1089.
1 parent 1e623c8 commit 263e39b

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

core/lib/src/config/mod.rs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,23 @@ impl FullConfig {
249249
Ok(config)
250250
}
251251

252+
/// Return the default configuration for all environments and marks the
253+
/// active environment (from `CONFIG_ENV`) as active. Overrides the defaults
254+
/// with values from the `ROCKET_{PARAM}` environment variables. Doesn't
255+
/// read any other sources.
256+
pub fn env_default() -> Result<FullConfig> {
257+
let mut config = Self::active_default_with_path(None)?;
258+
config.override_from_env()?;
259+
Ok(config)
260+
}
261+
252262
/// Return the default configuration for all environments and marks the
253263
/// active environment (from `CONFIG_ENV`) as active. This doesn't read
254264
/// `filename`, nor any other config values from any source; it simply uses
255265
/// `filename` to set up the config path property in the returned `Config`.
256-
pub fn active_default<'a, P: Into<Option<&'a Path>>>(filename: P) -> Result<FullConfig> {
266+
fn active_default_with_path(path: Option<&Path>) -> Result<FullConfig> {
257267
let mut defaults = HashMap::new();
258-
if let Some(path) = filename.into() {
268+
if let Some(path) = path {
259269
defaults.insert(Development, Config::default_from(Development, &path)?);
260270
defaults.insert(Staging, Config::default_from(Staging, &path)?);
261271
defaults.insert(Production, Config::default_from(Production, &path)?);
@@ -405,7 +415,7 @@ impl FullConfig {
405415
};
406416

407417
// Create a config with the defaults; set the env to the active one.
408-
let mut config = FullConfig::active_default(filename.as_ref())?;
418+
let mut config = FullConfig::active_default_with_path(Some(filename.as_ref()))?;
409419

410420
// Store all of the global overrides, if any, for later use.
411421
let mut global = None;
@@ -483,10 +493,8 @@ mod test {
483493
);
484494
}
485495

486-
fn active_default() -> Result<FullConfig> {
487-
let mut config = FullConfig::active_default(None)?;
488-
config.override_from_env()?;
489-
Ok(config)
496+
fn env_default() -> Result<FullConfig> {
497+
FullConfig::env_default()
490498
}
491499

492500
fn default_config(env: Environment) -> ConfigBuilder {
@@ -501,25 +509,25 @@ mod test {
501509
// First, without an environment. Should get development defaults on
502510
// debug builds and productions defaults on non-debug builds.
503511
env::remove_var(CONFIG_ENV);
504-
#[cfg(debug_assertions)] check_config!(active_default(), default_config(Development));
505-
#[cfg(not(debug_assertions))] check_config!(active_default(), default_config(Production));
512+
#[cfg(debug_assertions)] check_config!(env_default(), default_config(Development));
513+
#[cfg(not(debug_assertions))] check_config!(env_default(), default_config(Production));
506514

507515
// Now with an explicit dev environment.
508516
for env in &["development", "dev"] {
509517
env::set_var(CONFIG_ENV, env);
510-
check_config!(active_default(), default_config(Development));
518+
check_config!(env_default(), default_config(Development));
511519
}
512520

513521
// Now staging.
514522
for env in &["stage", "staging"] {
515523
env::set_var(CONFIG_ENV, env);
516-
check_config!(active_default(), default_config(Staging));
524+
check_config!(env_default(), default_config(Staging));
517525
}
518526

519527
// Finally, production.
520528
for env in &["prod", "production"] {
521529
env::set_var(CONFIG_ENV, env);
522-
check_config!(active_default(), default_config(Production));
530+
check_config!(env_default(), default_config(Production));
523531
}
524532
}
525533

@@ -531,7 +539,7 @@ mod test {
531539
for env in &["", "p", "pr", "pro", "prodo", " prod", "dev ", "!dev!", "🚀 "] {
532540
env::set_var(CONFIG_ENV, env);
533541
let err = ConfigError::BadEnv(env.to_string());
534-
assert!(active_default().err().map_or(false, |e| e == err));
542+
assert!(env_default().err().map_or(false, |e| e == err));
535543
}
536544

537545
// Test that a bunch of invalid environment names give the right error.
@@ -1094,12 +1102,12 @@ mod test {
10941102
// Check that it overrides the active config.
10951103
for env in &Environment::ALL {
10961104
env::set_var(CONFIG_ENV, env.to_string());
1097-
let rconfig = active_default().unwrap();
1105+
let rconfig = env_default().unwrap();
10981106
check_value!(&*key.to_lowercase(), val, rconfig.active());
10991107
}
11001108

11011109
// And non-active configs.
1102-
let rconfig = active_default().unwrap();
1110+
let rconfig = env_default().unwrap();
11031111
for env in &Environment::ALL {
11041112
check_value!(&*key.to_lowercase(), val, rconfig.get(*env));
11051113
}

core/lib/src/rocket.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,9 @@ impl Rocket {
344344

345345
/// Create a new `Rocket` application using the configuration information in
346346
/// `Rocket.toml`. If the file does not exist or if there is an I/O error
347-
/// reading the file, the defaults are used. See the
348-
/// [`config`](crate::config) documentation for more information on
349-
/// defaults.
347+
/// reading the file, the defaults, overridden by any environment-based
348+
/// paramparameters, are used. See the [`config`](crate::config)
349+
/// documentation for more information on defaults.
350350
///
351351
/// This method is typically called through the
352352
/// [`rocket::ignite()`](crate::ignite) alias.
@@ -368,9 +368,9 @@ impl Rocket {
368368
.or_else(|e| match e {
369369
ConfigError::IoError => {
370370
warn!("Failed to read 'Rocket.toml'. Using defaults.");
371-
Ok(FullConfig::active_default(None)?.take_active())
371+
Ok(FullConfig::env_default()?.take_active())
372372
}
373-
ConfigError::NotFound => Ok(FullConfig::active_default(None)?.take_active()),
373+
ConfigError::NotFound => Ok(FullConfig::env_default()?.take_active()),
374374
_ => Err(e)
375375
})
376376
.map(Rocket::configured)

0 commit comments

Comments
 (0)