From 154546a7b4eb36eb1e78fb814c5fe21c6030c2ee Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Sat, 9 May 2020 16:27:54 +0200 Subject: Split logic in multiple files! --- src/diplonat.rs | 15 ++++++++------- src/gw.rs | 13 +++++++++++++ src/main.rs | 5 ++--- 3 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 src/gw.rs 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"); -- cgit v1.2.3