Split logic in multiple files!

This commit is contained in:
Quentin 2020-05-09 16:27:54 +02:00
parent 41caf6090c
commit 154546a7b4
3 changed files with 23 additions and 10 deletions

View file

@ -1,18 +1,19 @@
use igd::Gateway;
use anyhow::{Result, Context}; use anyhow::{Result, Context};
use log::*;
use crate::*; use crate::*;
pub struct DiplonatContext { pub struct DiplonatContext {
pub config: config::DiplonatConfig, pub config: config::DiplonatConfig,
//pub gateway: igd::Gateway pub gateway: igd::aio::Gateway
} }
pub fn setup() -> Result<DiplonatContext> { pub async fn setup() -> Result<DiplonatContext> {
return Ok(DiplonatContext { let ctx = DiplonatContext {
config: config::load_env().context("Unable to read configuration from environment")?, 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 { pub fn listen() -> bool {

13
src/gw.rs Normal file
View file

@ -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<igd::aio::Gateway> {
let gw = search_gateway(Default::default())
.await
.context("Failed to find gateway")?;
info!("Gateway: {}", gw);
return Ok(gw);
}

View file

@ -3,17 +3,17 @@ use std::net::SocketAddrV4;
use log::*; use log::*;
use igd::aio::search_gateway;
use igd::PortMappingProtocol; use igd::PortMappingProtocol;
mod diplonat; mod diplonat;
mod config; mod config;
mod gw;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
pretty_env_logger::init(); pretty_env_logger::init();
let ctx = diplonat::setup().expect("Setup failed"); let ctx = diplonat::setup().await.expect("Setup failed");
diplonat::listen(); diplonat::listen();
/* /*
let url = format!("http://127.0.0.1:8500/v1/catalog/node/{}", config.consul_node_name); 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, Ok(g) => g,
Err(err) => return println!("Faild to find IGD: {}", err), Err(err) => return println!("Faild to find IGD: {}", err),
}; };
info!("Gateway: {}", gateway);
let service = format!("{}:{}", config.private_ip, 1234); let service = format!("{}:{}", config.private_ip, 1234);
let service: SocketAddrV4 = service.parse().expect("Invalid socket address"); let service: SocketAddrV4 = service.parse().expect("Invalid socket address");