Add logging for known env variables

This commit is contained in:
Daniel Sockwell 2020-01-05 18:00:11 -05:00
parent 4a2d08c693
commit a2adadfa8c
5 changed files with 60 additions and 7 deletions

View File

@ -9,7 +9,7 @@ use std::{
use strum_macros::{EnumString, EnumVariantNames};
from_env_var!(
/// The current environment, which controls what file to read other ENV vars from
/// The current environment, which controls what file to read other ENV vars from
let name = Env;
let default: EnvInner = EnvInner::Development;
let (env_var, allowed_values) = ("RUST_ENV", format!("one of: {:?}", EnvInner::variants()));
@ -29,7 +29,7 @@ from_env_var!(
/// How verbosely Flodgatt should log messages
let name = LogLevel;
let default: LogLevelInner = LogLevelInner::Warn;
let (env_var, allowed_values) = ("RUST_LOG", "a valid address (e.g., 127.0.0.1)".to_string());
let (env_var, allowed_values) = ("RUST_LOG", "a valid address (e.g., 127.0.0.1)".to_string());
let from_str = |s| LogLevelInner::from_str(s).ok();
);
from_env_var!(

View File

@ -9,7 +9,7 @@ pub use self::{
redis_cfg::RedisConfig,
redis_cfg_types::{RedisInterval, RedisNamespace},
};
use std::collections::HashMap;
use std::{collections::HashMap, fmt};
use url::Url;
pub struct EnvVar(pub HashMap<String, String>);
@ -19,12 +19,16 @@ impl std::ops::Deref for EnvVar {
&self.0
}
}
impl Clone for EnvVar {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl EnvVar {
pub fn new(vars: HashMap<String, String>) -> Self {
Self(vars)
}
fn update_with_url(mut self, url_str: &str) -> Self {
let url = Url::parse(url_str).unwrap();
let none_if_empty = |s: String| if s.is_empty() { None } else { Some(s) };
@ -53,7 +57,42 @@ impl EnvVar {
}
}
}
impl fmt::Display for EnvVar {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut result =
String::from("Flodgatt received the following known environmental variables:");
for env_var in [
"NODE_ENV",
"RUST_LOG",
"BIND",
"PORT",
"SOCKET",
"SSE_FREQ",
"WS_FREQ",
"DATABASE_URL",
"DB_USER",
"USER",
"DB_PORT",
"DB_HOST",
"DB_PASS",
"DB_NAME",
"DB_SSLMODE",
"REDIS_HOST",
"REDIS_USER",
"REDIS_PORT",
"REDIS_PASSWORD",
"REDIS_USER",
"REDIS_DB",
]
.iter()
{
if let Some(value) = self.get(&env_var.to_string()) {
result = format!("{}\n {}: {}", result, env_var, value)
}
}
write!(f, "{}", result)
}
}
#[macro_export]
macro_rules! maybe_update {
($name:ident; $item: tt:$type:ty) => (

View File

@ -39,8 +39,8 @@ impl PostgresConfig {
// TODO: add TLS support, remove `NoTls`
match env_vars.get("DATABASE_URL") {
Some(url) => {
log::warn!("DATABASE_URL env variable set. Connecting to Postgres with that URL and ignoring any values set in DB_HOST, DB_USER, DB_NAME, DB_PASS, or DB_PORT.");
PostgresConfig::from_url(Url::parse(url).unwrap())
log::warn!("DATABASE_URL env variable set. Connecting to Postgres with that URL and ignoring any values set in DB_HOST, DB_USER, DB_NAME, DB_PASS, or DB_PORT.");
Self::from_url(Url::parse(url).unwrap())
}
None => Self::default()
.maybe_update_user(env_vars.get("USER").map(String::from))

View File

@ -0,0 +1,12 @@
use crate::from_env_var;
from_env_var!(
/// The host address where Redis is running
let name = PgHost;
let default: IpAddr = IpAddr::V4("127.0.0.1".parse().expect("hardcoded"));
let (env_var, allowed_values) = ("", "a valid address (e.g., 127.0.0.1)".to_string());
let from_str = |s| match s {
"localhost" => Some(IpAddr::V4(Ipv4Addr::LOCALHOST)),
_ => s.parse().ok(),
};
);

View File

@ -15,8 +15,10 @@ fn main() {
Some(_) => err::die_with_msg("Unknown ENV variable specified.\n Valid options are: `production` or `development`."),
}).ok();
let env_vars_map: HashMap<_, _> = dotenv::vars().collect();
let env_vars = config::EnvVar(env_vars_map);
let env_vars = config::EnvVar::new(env_vars_map);
pretty_env_logger::init();
warn!("{}", env_vars.clone());
let redis_cfg = config::RedisConfig::from_env(env_vars.clone());
let cfg = config::DeploymentConfig::from_env(env_vars.clone());