hotfix: read env variables for config params with _ in their names
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:
Aravinth Manivannan 2022-11-16 17:20:34 +05:30
parent 8b821b7bde
commit 1fcded74c0
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88

View file

@ -19,11 +19,15 @@ use std::path::Path;
use config::{Config, ConfigError, Environment, File}; use config::{Config, ConfigError, Environment, File};
use derive_more::Display; use derive_more::Display;
use log::info;
use log::warn; use log::warn;
use serde::Deserialize; use serde::Deserialize;
use serde::Serialize; use serde::Serialize;
use url::Url; use url::Url;
const PREFIX: &str = "LPCONDUCTOR";
const SEPARATOR: &str = "__";
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
pub struct Server { pub struct Server {
pub port: u32, pub port: u32,
@ -72,10 +76,11 @@ impl Settings {
} else if Path::new(ETC).exists() { } else if Path::new(ETC).exists() {
s.merge(File::with_name(ETC))?; s.merge(File::with_name(ETC))?;
} else { } 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); 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))] #[cfg(not(tarpaulin_include))]
fn check_url(s: &Config) { fn check_url(s: &Config) {
let url = s let url = s