diff --git a/src/diplonat.rs b/src/diplonat.rs index 38b2f8a..c209da5 100644 --- a/src/diplonat.rs +++ b/src/diplonat.rs @@ -1,18 +1,19 @@ -use igd::Gateway; use anyhow::{Result, Context}; - +use log::*; use crate::*; pub struct DiplonatContext { pub config: config::DiplonatConfig, - //pub gateway: igd::Gateway + pub gateway: igd::aio::Gateway } -pub fn setup() -> Result { - return Ok(DiplonatContext { +pub async fn setup() -> Result { + let ctx = DiplonatContext { config: config::load_env().context("Unable to read configuration from environment")?, - //gateway: search_gateway(Default::default()).await - }); + gateway: gw::get_gateway().await? + }; + + return Ok(ctx); } pub fn listen() -> bool { diff --git a/src/gw.rs b/src/gw.rs new file mode 100644 index 0000000..f716a3f --- /dev/null +++ b/src/gw.rs @@ -0,0 +1,13 @@ +use igd::aio::Gateway; +use igd::aio::search_gateway; +use anyhow::{Result, Context}; +use log::*; + +pub async fn get_gateway() -> Result { + let gw = search_gateway(Default::default()) + .await + .context("Failed to find gateway")?; + + info!("Gateway: {}", gw); + return Ok(gw); +} diff --git a/src/main.rs b/src/main.rs index 471573b..91074d1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,17 +3,17 @@ use std::net::SocketAddrV4; use log::*; -use igd::aio::search_gateway; use igd::PortMappingProtocol; mod diplonat; mod config; +mod gw; #[tokio::main] async fn main() { pretty_env_logger::init(); - let ctx = diplonat::setup().expect("Setup failed"); + let ctx = diplonat::setup().await.expect("Setup failed"); diplonat::listen(); /* let url = format!("http://127.0.0.1:8500/v1/catalog/node/{}", config.consul_node_name); @@ -28,7 +28,6 @@ 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");