aboutsummaryrefslogtreecommitdiff
path: root/src/config.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs26
1 files changed, 17 insertions, 9 deletions
diff --git a/src/config.rs b/src/config.rs
index 2cf2061..14d18be 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -1,5 +1,5 @@
use std::env;
-use anyhow::{Result, anyhow};
+use anyhow::{Result, Context, anyhow};
use log::*;
pub struct DiplonatConfig {
@@ -10,16 +10,24 @@ pub struct DiplonatConfig {
}
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 epi = "DIPLONAT_PRIVATE_IP";
+ let ert = "DIPLONAT_REFRESH_TIME";
+ let eet = "DIPLONAT_EXPIRATION_TIME";
+ let ecnd = "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)?
+ private_ip: env::var(epi)
+ .with_context(|| format!("{} env var must be defined, eg: 192.168.0.18", epi))?,
+ refresh_time: 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: 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))?,
+ consul_node_name: env::var(ecnd)
+ .with_context(|| format!("{} env var must be defined", ecnd))?
};
if config.refresh_time * 2 > config.expiration_time {