aboutsummaryrefslogtreecommitdiff
path: root/src/config.rs
blob: 2cf20610d8a52e9fb08d406bfb561a7d3e78de27 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use std::env;
use anyhow::{Result, anyhow};
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> {
  let env_private_ip = "DIPLONAT_PRIVATE_IP";
  let env_refresh_time = "DIPLONAT_REFRESH_TIME";
  let env_expiration_time = "DIPLONAT_EXPIRATION_TIME";
  let env_consul_node_name = "DIPLONAT_CONSUL_NODE_NAME";

  let config = DiplonatConfig { 
    private_ip: env::var(env_private_ip)?, 
    refresh_time: env::var(env_refresh_time)?.parse()?, 
    expiration_time: env::var(env_expiration_time)?.parse()?, 
    consul_node_name: env::var(env_consul_node_name)?
  };

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

  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);
}