aboutsummaryrefslogblamecommitdiff
path: root/src/config.rs
blob: bbc1b337538c30fad9165dcca28efbb8e64ac2d9 (plain) (tree)







































                                                                                                                                                  





                                       







                                                               
use std::env;

use log::*;

pub struct DiplonatConfig {
   pub private_ip: String,
   pub consul_node_name: String,
   pub refresh_time: u32,
   pub expiration_time: u32
}

pub fn load_env() -> Result<DiplonatConfig, String> {
  let env_private_ip = "DIPLONAT_PRIVATE_IP";
  let private_ip = match env::var(env_private_ip) {
      Ok(val) => val,
      Err(e) => return Err(format!("unable to fetch environment variable {}: {}", env_private_ip, e)),
  };

  let env_refresh_time = "DIPLONAT_REFRESH_TIME";
  let refresh_time: u32 = match env::var(env_refresh_time) {
      Ok(val) => val.parse().unwrap(),
      Err(e) => return Err(format!("unable to fetch environment variable {}: {}", env_refresh_time,e))
  };
  
  let env_expiration_time = "DIPLONAT_EXPIRATION_TIME";
  let expiration_time: u32 = match env::var(env_expiration_time) {
      Ok(val) => val.parse().unwrap(),
      Err(e) => return Err(format!("unable to fetch environment variable {}: {}", env_expiration_time,e))
  };
  
  let env_consul_node_name = "DIPLONAT_CONSUL_NODE_NAME";
  let consul_node_name = match env::var(env_consul_node_name) {
      Ok(val) => val,
      Err(e) => return Err(format!("unable to fetch environment variable {}: {}", env_consul_node_name,e))
  };

  if refresh_time * 2 > expiration_time {
    return Err(format!("Expiration time (currently: {}s) must be twice bigger than refresh time (currently: {}s)", expiration_time, refresh_time))
  }

  let config = DiplonatConfig { 
    private_ip: private_ip, 
    refresh_time: refresh_time, 
    expiration_time: expiration_time, 
    consul_node_name: consul_node_name 
  };
  info!("Consul node name: {}", config.consul_node_name);
  info!("Private IP address: {}", config.private_ip);
  info!("Refresh time: {} seconds", config.refresh_time);
  info!("Expiration time: {} seconds", config.expiration_time);
  return Ok(config);
}