50 lines
1.8 KiB
Rust
50 lines
1.8 KiB
Rust
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{});
|
|
}
|
|
}
|
|
|
|
|