Skip to content

Instantly share code, notes, and snippets.

@ircnelson
Created June 22, 2020 06:23
Show Gist options
  • Save ircnelson/51e91b3a032de7d89b7612e98681a644 to your computer and use it in GitHub Desktop.
Save ircnelson/51e91b3a032de7d89b7612e98681a644 to your computer and use it in GitHub Desktop.
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