Don't read environment parameter from config file

This commit is contained in:
silverpill 2022-05-04 20:39:24 +00:00
parent 379116605f
commit e927ed83ff

View file

@ -3,7 +3,7 @@ use std::str::FromStr;
use log::{Level as LogLevel}; use log::{Level as LogLevel};
use rsa::RsaPrivateKey; use rsa::RsaPrivateKey;
use serde::{de, Deserialize, Deserializer}; use serde::Deserialize;
use url::Url; use url::Url;
use crate::activitypub::constants::ACTOR_KEY_SUFFIX; use crate::activitypub::constants::ACTOR_KEY_SUFFIX;
@ -24,6 +24,10 @@ pub enum Environment {
Production, Production,
} }
impl Default for Environment {
fn default() -> Self { Self::Development }
}
impl FromStr for Environment { impl FromStr for Environment {
type Err = ConversionError; type Err = ConversionError;
@ -37,19 +41,10 @@ impl FromStr for Environment {
} }
} }
fn environment_from_str<'de, D>(deserializer: D) -> Result<Environment, D::Error> struct EnvConfig {
where environment: Environment,
D: Deserializer<'de>, config_path: String,
{ crate_version: String,
let s: String = Deserialize::deserialize(deserializer)?;
Environment::from_str(&s).map_err(de::Error::custom)
}
#[derive(Clone)]
pub struct EnvConfig {
pub environment: Option<Environment>,
pub config_path: String,
pub crate_version: String,
} }
fn parse_env() -> EnvConfig { fn parse_env() -> EnvConfig {
@ -57,7 +52,8 @@ fn parse_env() -> EnvConfig {
dotenv::dotenv().ok(); dotenv::dotenv().ok();
let environment_str = std::env::var("ENVIRONMENT").ok(); let environment_str = std::env::var("ENVIRONMENT").ok();
let environment = environment_str let environment = environment_str
.map(|val| Environment::from_str(&val).expect("invalid environment type")); .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("config.yaml".to_string()); .unwrap_or("config.yaml".to_string());
let crate_version = env!("CARGO_PKG_VERSION").to_string(); let crate_version = env!("CARGO_PKG_VERSION").to_string();
@ -68,8 +64,6 @@ fn parse_env() -> EnvConfig {
} }
} }
fn default_environment() -> Environment { Environment::Development }
fn default_log_level() -> LogLevel { LogLevel::Info } fn default_log_level() -> LogLevel { LogLevel::Info }
fn default_post_character_limit() -> usize { 2000 } fn default_post_character_limit() -> usize { 2000 }
@ -96,8 +90,7 @@ impl BlockchainConfig {
#[derive(Clone, Deserialize)] #[derive(Clone, Deserialize)]
pub struct Config { pub struct Config {
#[serde(default = "default_environment")] #[serde(skip)]
#[serde(deserialize_with = "environment_from_str")]
pub environment: Environment, pub environment: Environment,
#[serde(skip)] #[serde(skip)]
@ -244,9 +237,8 @@ pub fn parse_config() -> Config {
.expect("failed to load config file"); .expect("failed to load config file");
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");
// Override environment parameter in config if env variable is set // Set parameters from environment
config.environment = env.environment.unwrap_or(config.environment); config.environment = env.environment;
// Set version
config.version = env.crate_version; config.version = env.crate_version;
// Validate config // Validate config
if !config.storage_dir.exists() { if !config.storage_dir.exists() {