aboutsummaryrefslogtreecommitdiff
path: root/src/environment.rs
diff options
context:
space:
mode:
authorQuentin Dufour <quentin@deuxfleurs.fr>2020-05-22 18:41:13 +0200
committerQuentin Dufour <quentin@deuxfleurs.fr>2020-05-22 18:41:13 +0200
commite1d0eadb9dc5e2e3bea8bc48f7b33ad9ef632554 (patch)
treed1e891b87684c7907ff1245ebd5ad403804f6fd9 /src/environment.rs
parentdeeecd93e1fc5545e6e0280d1799436638e84a38 (diff)
downloaddiplonat-e1d0eadb9dc5e2e3bea8bc48f7b33ad9ef632554.tar.gz
diplonat-e1d0eadb9dc5e2e3bea8bc48f7b33ad9ef632554.zip
WIP software
Diffstat (limited to 'src/environment.rs')
-rw-r--r--src/environment.rs56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/environment.rs b/src/environment.rs
new file mode 100644
index 0000000..335fa37
--- /dev/null
+++ b/src/environment.rs
@@ -0,0 +1,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);
+ }
+}
+
+