Refactor parse_env() function
This commit is contained in:
parent
200675464e
commit
8dfb040b5f
1 changed files with 9 additions and 7 deletions
|
@ -23,8 +23,8 @@ use super::environment::Environment;
|
||||||
use super::MITRA_VERSION;
|
use super::MITRA_VERSION;
|
||||||
|
|
||||||
struct EnvConfig {
|
struct EnvConfig {
|
||||||
environment: Environment,
|
|
||||||
config_path: String,
|
config_path: String,
|
||||||
|
environment: Option<Environment>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "production")]
|
#[cfg(feature = "production")]
|
||||||
|
@ -35,15 +35,13 @@ const DEFAULT_CONFIG_PATH: &str = "config.yaml";
|
||||||
fn parse_env() -> EnvConfig {
|
fn parse_env() -> EnvConfig {
|
||||||
dotenv::from_filename(".env.local").ok();
|
dotenv::from_filename(".env.local").ok();
|
||||||
dotenv::dotenv().ok();
|
dotenv::dotenv().ok();
|
||||||
let environment_str = std::env::var("ENVIRONMENT").ok();
|
|
||||||
let environment = environment_str
|
|
||||||
.map(|val| Environment::from_str(&val).expect("invalid environment type"))
|
|
||||||
.unwrap_or_default();
|
|
||||||
let config_path = std::env::var("CONFIG_PATH")
|
let config_path = std::env::var("CONFIG_PATH")
|
||||||
.unwrap_or(DEFAULT_CONFIG_PATH.to_string());
|
.unwrap_or(DEFAULT_CONFIG_PATH.to_string());
|
||||||
|
let environment = std::env::var("ENVIRONMENT").ok()
|
||||||
|
.map(|val| Environment::from_str(&val).expect("invalid environment type"));
|
||||||
EnvConfig {
|
EnvConfig {
|
||||||
environment,
|
|
||||||
config_path,
|
config_path,
|
||||||
|
environment,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,6 +53,7 @@ fn default_post_character_limit() -> usize { 2000 }
|
||||||
|
|
||||||
#[derive(Clone, Deserialize)]
|
#[derive(Clone, Deserialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
|
// Properties auto-populated from the environment
|
||||||
#[serde(skip)]
|
#[serde(skip)]
|
||||||
pub environment: Environment,
|
pub environment: Environment,
|
||||||
|
|
||||||
|
@ -250,8 +249,11 @@ pub fn parse_config() -> Config {
|
||||||
let mut config = serde_yaml::from_str::<Config>(&config_yaml)
|
let mut config = serde_yaml::from_str::<Config>(&config_yaml)
|
||||||
.expect("invalid yaml data");
|
.expect("invalid yaml data");
|
||||||
// Set parameters from environment
|
// Set parameters from environment
|
||||||
config.environment = env.environment;
|
|
||||||
config.config_path = env.config_path;
|
config.config_path = env.config_path;
|
||||||
|
if let Some(environment) = env.environment {
|
||||||
|
// Overwrite default only if ENVIRONMENT variable is set
|
||||||
|
config.environment = environment;
|
||||||
|
};
|
||||||
|
|
||||||
// Validate config
|
// Validate config
|
||||||
if !config.storage_dir.exists() {
|
if !config.storage_dir.exists() {
|
||||||
|
|
Loading…
Reference in a new issue