diff options
author | Quentin Dufour <quentin@deuxfleurs.fr> | 2020-05-21 22:25:33 +0200 |
---|---|---|
committer | Quentin Dufour <quentin@deuxfleurs.fr> | 2020-05-21 22:25:33 +0200 |
commit | 8c43611eb5bbaeb42f19da8d8ed521df208bfada (patch) | |
tree | c207e950744f451424e139e97aaf852c2d8ff949 /src/environment_adapter.rs | |
parent | 4da459ce8ba49f98084a8ce32f600470ff883ec5 (diff) | |
download | diplonat-8c43611eb5bbaeb42f19da8d8ed521df208bfada.tar.gz diplonat-8c43611eb5bbaeb42f19da8d8ed521df208bfada.zip |
Broken Diplonat
Diffstat (limited to 'src/environment_adapter.rs')
-rw-r--r-- | src/environment_adapter.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/environment_adapter.rs b/src/environment_adapter.rs new file mode 100644 index 0000000..e4fad70 --- /dev/null +++ b/src/environment_adapter.rs @@ -0,0 +1,50 @@ +use std::env; +use tokio::sync::broadcast; +use anyhow::{Result, Context, anyhow}; +use log::*; +use crate::diplonat::*; +use crate::node_state::*; +use std::cell::Cell; + +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 EnvironmentAdapter {} + +impl EnvironmentAdapter { + pub async fn new(ns: &Cell<NodeState>, _: &broadcast::Sender<()>) -> Result<Self> { + ns.consul_node_name = Some(match env::var(ecu) { + Ok(e) => e, + Err(_) => "http://127.0.0.1:8500".to_string() + }); + + ns.private_ip = Some(env::var(epi) + .with_context(|| format!("{} env var must be defined, eg: 192.168.0.18", epi))?); + + ns.refresh_time = Some(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))?); + + ns.expiration_time = Some(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))?); + + ns.consul_node_name = Some(env::var(ecnd) + .with_context(|| format!("{} env var must be defined", ecnd))?); + + match (ns.refresh_time, ns.expiration_time) { + (Some(rt), Some(et)) if rt * 2 <= et => debug!("Checked refresh time is lower than expiration time"), + (Some(rt), Some(et)) => return Err(anyhow!("Expiration time (currently: {}s) must be twice bigger than refresh time (currently: {}s)", rt, et)), + _ => return Err(anyhow!("Please define refresh time and expiration time")) + } + + return Ok(Self{}); + } +} + + |