Created
June 22, 2020 06:23
-
-
Save ircnelson/51e91b3a032de7d89b7612e98681a644 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use config::{Config, ConfigError, Environment, File}; | |
use serde_derive::Deserialize; | |
use std::env; | |
#[derive(Deserialize)] | |
pub struct ServerSettings { | |
pub host: String, | |
pub port: i32, | |
} | |
#[derive(Deserialize)] | |
pub struct Settings { | |
pub server: ServerSettings, | |
} | |
impl Settings { | |
pub fn new() -> Result<Self, ConfigError> { | |
let env = env::var("environment").unwrap_or("development".into()); | |
let mut cfg = Config::new(); | |
cfg.merge(File::with_name("settings"))?; | |
cfg.merge(File::with_name(&format!("settings_{}", env)).required(false))?; | |
cfg.merge(Environment::new())?; | |
cfg.try_into() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment