aboutsummaryrefslogtreecommitdiff
path: root/src/environment.rs
blob: 335fa37099f67d68c0296eb0f5e40147483a8f71 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use std::env;
use anyhow::{Result, Context, anyhow};
use std::time::Duration;
use log::*;

const epi: &'static str = "DIPLONAT_PRIVATE_IP";
const ert: &'static str = "DIPLONAT_REFRESH_TIME";
const eet: &'static str = "DIPLONAT_EXPIRATION_TIME";
const ecnd: &'static str = "DIPLONAT_CONSUL_NODE_NAME";
const ecu: &'static str = "DIPLONAT_CONSUL_URL";

pub struct Environment {
   pub consul_node_name: String,
   pub consul_url: String,
   
   pub refresh_time: Duration,
   pub expiration_time: Duration,

   pub private_ip: String,
}

/* @FIXME: Rewrite with Serde Envi */
impl Environment {
  pub fn new() -> Result<Self> {
    let ctx = Self {
      consul_url: match env::var(ecu) { Ok(e) => e, Err(_) => "http://127.0.0.1:8500".to_string() },
      consul_node_name: env::var(ecnd).with_context(|| format!("{} env var must be defined", ecnd))?,
      private_ip: env::var(epi).with_context(|| format!("{} env var must be defined, eg: 192.168.0.18", epi))?,
      refresh_time: Duration::from_secs(env::var(ert)
        .with_context(|| format!("{} env var must be defined, eg: 60", ert))?
        .parse()
        .with_context(|| format!("{} env var must be an integer, eg: 60", ert))?),
      expiration_time: Duration::from_secs(env::var(eet)
        .with_context(|| format!("{} env var must be defined, eg: 300", eet))?
        .parse()
        .with_context(|| format!("{} env var must be an integer, eg: 300", eet))?),
    };

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

    info!("Consul URL: {:#?}", ctx.consul_url);
    info!("Consul node name: {:#?}", ctx.consul_node_name);
    info!("Private IP address: {:#?}", ctx.private_ip);
    info!("Refresh time: {:#?} seconds", ctx.refresh_time.as_secs());
    info!("Expiration time: {:#?} seconds", ctx.expiration_time.as_secs());
    
    return Ok(ctx);
  }
}