diplonat/src/config.rs

37 lines
1.2 KiB
Rust

use std::env;
use anyhow::{Result, anyhow};
use log::*;
pub struct DiplonatConfig {
pub private_ip: String,
pub consul_node_name: String,
pub refresh_time: u32,
pub expiration_time: u32
}
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 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)?
};
if config.refresh_time * 2 > config.expiration_time {
return Err(anyhow!("Expiration time (currently: {}s) must be twice bigger than refresh time (currently: {}s)", config.expiration_time, config.refresh_time))
}
info!("Consul node name: {}", config.consul_node_name);
info!("Private IP address: {}", config.private_ip);
info!("Refresh time: {} seconds", config.refresh_time);
info!("Expiration time: {} seconds", config.expiration_time);
return Ok(config);
}