2020-04-13 22:03:06 +02:00
|
|
|
pub use {deployment_cfg::Deployment, postgres_cfg::Postgres, redis_cfg::Redis};
|
|
|
|
|
|
|
|
use self::environmental_variables::EnvVar;
|
2020-04-15 02:37:49 +02:00
|
|
|
use super::err::FatalErr;
|
2020-04-13 22:03:06 +02:00
|
|
|
use hashbrown::HashMap;
|
|
|
|
use std::env;
|
|
|
|
|
|
|
|
mod deployment_cfg;
|
|
|
|
mod deployment_cfg_types;
|
|
|
|
mod environmental_variables;
|
|
|
|
mod postgres_cfg;
|
|
|
|
mod postgres_cfg_types;
|
|
|
|
mod redis_cfg;
|
|
|
|
mod redis_cfg_types;
|
|
|
|
|
2020-04-15 02:37:49 +02:00
|
|
|
type Result<T> = std::result::Result<T, FatalErr>;
|
|
|
|
|
|
|
|
pub fn merge_dotenv() -> Result<()> {
|
|
|
|
let env_file = match env::var("ENV").ok().as_deref() {
|
2020-04-13 22:03:06 +02:00
|
|
|
Some("production") => ".env.production",
|
|
|
|
Some("development") | None => ".env",
|
2020-04-15 02:37:49 +02:00
|
|
|
Some(v) => Err(FatalErr::config("ENV", v, "`production` or `development`"))?,
|
|
|
|
};
|
|
|
|
let res = dotenv::from_filename(env_file);
|
|
|
|
|
|
|
|
if let Ok(log_level) = env::var("RUST_LOG") {
|
|
|
|
if res.is_err() && ["warn", "info", "trace", "debug"].contains(&log_level.as_str()) {
|
|
|
|
eprintln!(
|
|
|
|
" WARN: could not load environmental variables from {:?}\n\
|
|
|
|
{:8}Are you in the right directory? Proceeding with variables from the environment.",
|
|
|
|
env::current_dir().unwrap_or_else(|_|"./".into()).join(env_file), ""
|
|
|
|
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-04-13 22:03:06 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-04-15 02:37:49 +02:00
|
|
|
#[allow(clippy::implicit_hasher)]
|
|
|
|
pub fn from_env<'a>(
|
|
|
|
env_vars: HashMap<String, String>,
|
|
|
|
) -> Result<(Postgres, Redis, Deployment<'a>)> {
|
2020-04-13 22:03:06 +02:00
|
|
|
let env_vars = EnvVar::new(env_vars);
|
2020-04-15 02:37:49 +02:00
|
|
|
log::info!(
|
|
|
|
"Flodgatt received the following environmental variables:{}",
|
|
|
|
&env_vars
|
|
|
|
);
|
|
|
|
|
|
|
|
let pg_cfg = Postgres::from_env(env_vars.clone())?;
|
|
|
|
log::info!("Configuration for {:#?}", &pg_cfg);
|
|
|
|
let redis_cfg = Redis::from_env(env_vars.clone())?;
|
|
|
|
log::info!("Configuration for {:#?},", &redis_cfg);
|
|
|
|
let deployment_cfg = Deployment::from_env(&env_vars)?;
|
|
|
|
log::info!("Configuration for {:#?}", &deployment_cfg);
|
|
|
|
|
|
|
|
Ok((pg_cfg, redis_cfg, deployment_cfg))
|
2020-04-13 22:03:06 +02:00
|
|
|
}
|