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 { - 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 { diff --git a/src/diplonat.rs b/src/diplonat.rs index 089a5c7..38b2f8a 100644 --- a/src/diplonat.rs +++ b/src/diplonat.rs @@ -1,5 +1,5 @@ use igd::Gateway; -use anyhow::Result; +use anyhow::{Result, Context}; use crate::*; @@ -10,11 +10,11 @@ pub struct DiplonatContext { pub fn setup() -> Result { return Ok(DiplonatContext { - config: config::load_env()?, + config: config::load_env().context("Unable to read configuration from environment")?, //gateway: search_gateway(Default::default()).await }); } -pub fn dloop() -> bool { +pub fn listen() -> bool { return true; } diff --git a/src/main.rs b/src/main.rs index a4c4e21..471573b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,8 @@ mod config; async fn main() { pretty_env_logger::init(); - let ctx = diplonat::setup().expect("Setup failed:"); + let ctx = diplonat::setup().expect("Setup failed"); + diplonat::listen(); /* let url = format!("http://127.0.0.1:8500/v1/catalog/node/{}", config.consul_node_name); let resp = reqwest::get(&url)