Skip to content
This repository has been archived by the owner on Aug 25, 2021. It is now read-only.

Commit

Permalink
Refactor config values merging (#24)
Browse files Browse the repository at this point in the history
Additionally:
- fix config tests
  • Loading branch information
evdokimovs authored May 22, 2019
1 parent 0c8a3aa commit 55ca927
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 53 deletions.
5 changes: 0 additions & 5 deletions config.default.toml

This file was deleted.

20 changes: 20 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
[server]
# IP address to bind HTTP server to.
#
# Default:
# bind_ip = "0.0.0.0"

# Port to bind HTTP server to.
#
# Default:
# bind_port = 8080




[rpc]
# Duration, after which remote RPC client will be considered idle if no
# heartbeat messages received.
#
# Default:
# idle_timeout = "10s"

# Duration, after which the server deletes the client session if
# the remote RPC client does not reconnect after it is idle.
#
# Default:
# reconnect_timeout = "10s"
75 changes: 27 additions & 48 deletions src/conf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@ pub mod server;

use std::env;

use config::{
Config, ConfigError, Environment, File, FileFormat, Source, Value,
};
use config::{Config, Environment, File};
use failure::Error;
use serde::{Deserialize, Serialize};

pub use self::rpc::Rpc;
pub use self::server::Server;

use std::collections::HashMap;
pub use self::{rpc::Rpc, server::Server};

/// CLI argument that is responsible for holding application configuration
/// file path.
Expand All @@ -25,25 +20,13 @@ static APP_CONF_PATH_ENV_VAR_NAME: &str = "MEDEA_CONF";

/// Holds application config.
#[derive(Clone, Debug, Deserialize, Serialize, Default)]
#[serde(default)]
pub struct Conf {
/// HTTP server settings.
pub rpc: rpc::Rpc,
/// RPC connection settings.
pub server: server::Server,
}

/// Allows merging [`Conf`] into [`config::Config`].
// TODO: Remove after the following issue is resolved:
// https://github.com/mehcode/config-rs/issues/60#issuecomment-443241600
impl Source for Conf {
fn clone_into_box(&self) -> Box<Source + Send + Sync> {
Box::new((*self).clone())
}
pub rpc: rpc::Rpc,

fn collect(&self) -> Result<HashMap<String, Value>, ConfigError> {
let serialized = toml::to_string(self).unwrap();
File::from_str(serialized.as_str(), FileFormat::Toml).collect()
}
/// HTTP server settings.
pub server: server::Server,
}

impl Conf {
Expand All @@ -54,19 +37,15 @@ impl Conf {
/// parameter or environment variable;
/// - environment variables.
pub fn parse() -> Result<Self, Error> {
// TODO: use Config::try_from(&Self::default()) when the issue is fixed:
// https://github.com/mehcode/config-rs/issues/60
let mut cfg = Config::new();
cfg.merge(Self::default())?;

if let Some(path) = get_conf_file_name(env::args()) {
cfg.merge(File::with_name(&path))?;
}

cfg.merge(Environment::with_prefix("MEDEA").separator("."))?;

let s: Self = cfg.try_into()?;
Ok(s)
Ok(cfg.try_into()?)
}
}

Expand All @@ -91,17 +70,22 @@ where
}

#[cfg(test)]
mod get_conf_file_name_spec {
mod tests {
use serial_test_derive::serial;
use std::{fs, time::Duration};

use super::*;

#[test]
fn none_if_nothing_is_set() {
#[serial]
fn get_conf_file_name_spec_none_if_nothing_is_set() {
env::remove_var(APP_CONF_PATH_ENV_VAR_NAME);
assert_eq!(get_conf_file_name(vec![]), None);
}

#[test]
fn none_if_empty() {
#[serial]
fn get_conf_file_name_spec_none_if_empty() {
env::set_var(APP_CONF_PATH_ENV_VAR_NAME, "env_path");
assert_eq!(
get_conf_file_name(vec![
Expand All @@ -110,16 +94,20 @@ mod get_conf_file_name_spec {
]),
None,
);
env::remove_var(APP_CONF_PATH_ENV_VAR_NAME);
}

#[test]
fn env_if_set() {
#[serial]
fn get_conf_file_name_spec_env_if_set() {
env::set_var(APP_CONF_PATH_ENV_VAR_NAME, "env_path");
assert_eq!(get_conf_file_name(vec![]), Some("env_path".to_owned()));
env::remove_var(APP_CONF_PATH_ENV_VAR_NAME);
}

#[test]
fn arg_if_set() {
#[serial]
fn get_conf_file_name_spec_arg_if_set() {
env::remove_var(APP_CONF_PATH_ENV_VAR_NAME);
assert_eq!(
get_conf_file_name(vec![
Expand All @@ -131,7 +119,8 @@ mod get_conf_file_name_spec {
}

#[test]
fn arg_is_prioritized() {
#[serial]
fn get_conf_file_name_spec_arg_is_prioritized() {
env::set_var(APP_CONF_PATH_ENV_VAR_NAME, "env_path");
assert_eq!(
get_conf_file_name(vec![
Expand All @@ -140,20 +129,12 @@ mod get_conf_file_name_spec {
]),
Some("arg_path".to_owned()),
);
env::remove_var(APP_CONF_PATH_ENV_VAR_NAME);
}
}

#[cfg(test)]
mod conf_parse_spec {
use std::{fs, time::Duration};

use serial_test_derive::serial;

use super::*;

#[test]
#[serial]
fn file_overrides_defaults() {
fn conf_parse_spec_file_overrides_defaults() {
let defaults = Conf::default();
let test_config_file_path = "test_config.toml";

Expand All @@ -170,11 +151,9 @@ mod conf_parse_spec {
assert_ne!(new_config.rpc.idle_timeout, defaults.rpc.idle_timeout);
}

// TODO: This test seems to pollute environment and might
// fail from time to time.
#[test]
#[serial]
fn env_overrides_defaults() {
fn conf_parse_spec_env_overrides_defaults() {
let defaults = Conf::default();

env::set_var("MEDEA_RPC.IDLE_TIMEOUT", "46s");
Expand All @@ -187,7 +166,7 @@ mod conf_parse_spec {

#[test]
#[serial]
fn env_overrides_file() {
fn conf_parse_spec_env_overrides_file() {
let test_config_file_path = "test_config.toml";

let data = format!("[rpc]\nidle_timeout = \"47s\"");
Expand Down
1 change: 1 addition & 0 deletions src/conf/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::time::Duration;

/// RPC connection settings.
#[derive(Clone, Debug, Deserialize, Serialize, SmartDefault)]
#[serde(default)]
pub struct Rpc {
/// Duration, after which remote RPC client will be considered idle if no
/// heartbeat messages received. Defaults to `10s`.
Expand Down
4 changes: 4 additions & 0 deletions src/conf/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use smart_default::*;

/// HTTP server settings.
#[derive(Clone, Debug, Deserialize, Serialize, SmartDefault)]
#[serde(default)]
pub struct Server {
/// IP address to bind HTTP server to. Defaults to `0.0.0.0`.
#[default(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)))]
Expand Down Expand Up @@ -48,6 +49,9 @@ mod server_spec {

let env_conf = Conf::parse().unwrap();

env::remove_var("MEDEA_SERVER.BIND_IP");
env::remove_var("MEDEA_SERVER.BIND_PORT");

assert_ne!(default_conf.server.bind_ip, env_conf.server.bind_ip);
assert_ne!(default_conf.server.bind_port, env_conf.server.bind_port);

Expand Down

0 comments on commit 55ca927

Please sign in to comment.