hotfix: read env variables for config params with _
in their names
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ref: https://github.com/mehcode/config-rs/issues/391
This commit is contained in:
parent
8b821b7bde
commit
1fcded74c0
1 changed files with 32 additions and 2 deletions
|
@ -19,11 +19,15 @@ use std::path::Path;
|
|||
|
||||
use config::{Config, ConfigError, Environment, File};
|
||||
use derive_more::Display;
|
||||
use log::info;
|
||||
use log::warn;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
use url::Url;
|
||||
|
||||
const PREFIX: &str = "LPCONDUCTOR";
|
||||
const SEPARATOR: &str = "__";
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct Server {
|
||||
pub port: u32,
|
||||
|
@ -72,10 +76,11 @@ impl Settings {
|
|||
} else if Path::new(ETC).exists() {
|
||||
s.merge(File::with_name(ETC))?;
|
||||
} else {
|
||||
log::warn!("configuration file not found");
|
||||
warn!("configuration file not found");
|
||||
}
|
||||
|
||||
s.merge(Environment::with_prefix("LPCONDUCTOR").separator("_"))?;
|
||||
s.merge(Environment::with_prefix(PREFIX).separator(SEPARATOR))?;
|
||||
set_separator_field(&mut s);
|
||||
|
||||
check_url(&s);
|
||||
|
||||
|
@ -95,6 +100,31 @@ impl Settings {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(not(tarpaulin_include))]
|
||||
fn set_separator_field(s: &mut Config) {
|
||||
// ref: https://github.com/mehcode/config-rs/issues/391
|
||||
|
||||
fn from_env(s: &mut Config, env_name: &str, config_name: &str) {
|
||||
if let Ok(val) = env::var(env_name) {
|
||||
info!("Overriding {config_name} with data from env var {env_name}");
|
||||
s.set(config_name, val).unwrap_or_else(|_| panic!(
|
||||
"Couldn't set {config_name} from env var {env_name}"
|
||||
));
|
||||
}
|
||||
}
|
||||
from_env(s, &format!("{PREFIX}{SEPARATOR}SOURCE_CODE"), "source_code");
|
||||
from_env(
|
||||
s,
|
||||
&format!("{PREFIX}{SEPARATOR}SERVER{SEPARATOR}URL_PREFIX"),
|
||||
"server.url_prefix",
|
||||
);
|
||||
from_env(
|
||||
s,
|
||||
&format!("{PREFIX}{SEPARATOR}SERVER{SEPARATOR}PROXY_HAS_TLS"),
|
||||
"server.proxy_has_tls",
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(not(tarpaulin_include))]
|
||||
fn check_url(s: &Config) {
|
||||
let url = s
|
||||
|
|
Loading…
Reference in a new issue