From 643d6980e5c0f04928edf05219f735495e5589a5 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Sat, 9 May 2020 15:33:02 +0200 Subject: Start to modularize --- src/config.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 55 ++++++++----------------------------------------------- 2 files changed, 57 insertions(+), 47 deletions(-) create mode 100644 src/config.rs (limited to 'src') diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..950f4fe --- /dev/null +++ b/src/config.rs @@ -0,0 +1,49 @@ +use std::env; + +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 { + let env_private_ip = "DIPLONAT_PRIVATE_IP"; + let private_ip = match env::var(env_private_ip) { + Ok(val) => val, + Err(e) => return Err(format!("unable to fetch environment variable {}: {}", env_private_ip, e)), + }; + + let env_refresh_time = "DIPLONAT_REFRESH_TIME"; + let refresh_time: u32 = match env::var(env_refresh_time) { + Ok(val) => val.parse().unwrap(), + Err(e) => return Err(format!("unable to fetch environment variable {}: {}", env_refresh_time,e)) + }; + + let env_expiration_time = "DIPLONAT_EXPIRATION_TIME"; + let expiration_time: u32 = match env::var(env_expiration_time) { + Ok(val) => val.parse().unwrap(), + Err(e) => return Err(format!("unable to fetch environment variable {}: {}", env_expiration_time,e)) + }; + + let env_consul_node_name = "DIPLONAT_CONSUL_NODE_NAME"; + let consul_node_name = match env::var(env_consul_node_name) { + Ok(val) => val, + Err(e) => return Err(format!("unable to fetch environment variable {}: {}", env_consul_node_name,e)) + }; + + if refresh_time * 2 > expiration_time { + return Err(format!("Expiration time (currently: {}s) must be twice bigger than refresh time (currently: {}s)", expiration_time, refresh_time)) + } + + let config = DiplonatConfig { private_ip: private_ip, refresh_time: refresh_time, expiration_time: expiration_time, consul_node_name: consul_node_name }; + 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); +} + + diff --git a/src/main.rs b/src/main.rs index 09e9ecd..4a08ecc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,58 +1,18 @@ -use std::env; use std::net::SocketAddrV4; +//use std::collections::HashMap; + +use log::*; use igd::aio::search_gateway; use igd::PortMappingProtocol; -use std::collections::HashMap; - -struct DiplonatConfig { - private_ip: String, - consul_node_name: String, - refresh_time: u32, - expiration_time: u32 -} - -fn fetch_configuration() -> Result { - let env_private_ip = "DIPLONAT_PRIVATE_IP"; - let private_ip = match env::var(env_private_ip) { - Ok(val) => val, - Err(e) => return Err(format!("unable to fetch environment variable {}: {}", env_private_ip, e)), - }; - - let env_refresh_time = "DIPLONAT_REFRESH_TIME"; - let refresh_time: u32 = match env::var(env_refresh_time) { - Ok(val) => val.parse().unwrap(), - Err(e) => return Err(format!("unable to fetch environment variable {}: {}", env_refresh_time,e)) - }; - - let env_expiration_time = "DIPLONAT_EXPIRATION_TIME"; - let expiration_time: u32 = match env::var(env_expiration_time) { - Ok(val) => val.parse().unwrap(), - Err(e) => return Err(format!("unable to fetch environment variable {}: {}", env_expiration_time,e)) - }; - - let env_consul_node_name = "DIPLONAT_CONSUL_NODE_NAME"; - let consul_node_name = match env::var(env_consul_node_name) { - Ok(val) => val, - Err(e) => return Err(format!("unable to fetch environment variable {}: {}", env_consul_node_name,e)) - }; - - if refresh_time * 2 > expiration_time { - return Err(format!("Expiration time (currently: {}s) must be twice bigger than refresh time (currently: {}s)", expiration_time, refresh_time)) - } - - let config = DiplonatConfig { private_ip: private_ip, refresh_time: refresh_time, expiration_time: expiration_time, consul_node_name: consul_node_name }; - println!("\tConsul node name: {}", config.consul_node_name); - println!("\tPrivate IP address: {}", config.private_ip); - println!("\tRefresh time: {} seconds", config.refresh_time); - println!("\tExpiration time: {} seconds", config.expiration_time); - return Ok(config); -} +mod config; #[tokio::main] async fn main() { - let config = match fetch_configuration() { + pretty_env_logger::init(); + + let config = match config::load_env() { Ok(val) => val, Err(e) => return println!("unable to build configuration: {}", e), }; @@ -69,6 +29,7 @@ async fn main() { Ok(g) => g, Err(err) => return println!("Faild to find IGD: {}", err), }; + info!("Gateway: {}", gateway); let service = format!("{}:{}", config.private_ip, 1234); let service: SocketAddrV4 = service.parse().expect("Invalid socket address"); -- cgit v1.2.3