From a89aafec469db6c892b77e02e90b425ecd465d5a Mon Sep 17 00:00:00 2001 From: Noel Date: Tue, 5 Apr 2022 04:45:24 -0700 Subject: [PATCH] feat: properly add host support, fix env support --- Cargo.toml | 2 +- src/config.rs | 15 ++++++++++++--- src/main.rs | 19 ++++++++++++++----- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 30e8f77..c43fa21 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ [package] name = "sentry-worker" -version = "0.1.1" +version = "0.1.2" edition = "2021" authors = ["Noel "] diff --git a/src/config.rs b/src/config.rs index 49b9dfd..6567aef 100644 --- a/src/config.rs +++ b/src/config.rs @@ -44,7 +44,10 @@ fn env_to_config() -> Config { sentry_dsn, port: if port.is_some() { let p = port.unwrap(); - Some(p.parse::().unwrap()) + Some( + p.parse::() + .expect("Unable to parse String -> u16 for http port."), + ) } else { None }, @@ -53,6 +56,12 @@ fn env_to_config() -> Config { } pub static CONFIG: Lazy = Lazy::new(|| { - let contents = read_to_string("config.toml").expect("Unable to open config.toml :("); - toml::from_str(&contents).unwrap_or(env_to_config()) + let res = read_to_string("config.toml"); + + match res { + Ok(contents) => { + toml::from_str::(&contents).expect("Unable to parse 'config.toml' contents.") + } + Err(_) => env_to_config(), + } }); diff --git a/src/main.rs b/src/main.rs index 06753bc..8e6151a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,7 @@ use ansi_term::Colour::RGB; use chrono::Local; use fern::Dispatch; use log::{debug, info}; -use std::{collections::HashMap, env::var, thread}; +use std::{collections::HashMap, env::var, net::SocketAddr, thread}; use warp::Filter; mod config; @@ -41,15 +41,24 @@ async fn main() -> Result<()> { .and(warp::body::json::>()) .and_then(routes::sentry); - let port = config::CONFIG.port.clone(); + let port = config::CONFIG.port.clone().unwrap_or(3939); let routes = warp::any() .and(index.or(sentry)) .with(warp::log("sentry::http")); - warp::serve(routes) - .run(([0, 0, 0, 0], port.unwrap_or(3939))) - .await; + // Check if we can parse the config host to a SocketAddr. + let host = config::CONFIG.host.clone(); + let addr = if let Some(h) = host { + format!("{}:{}", h, port) + .parse::() + .expect(format!("Unable to parse host '{}' -> SocketAddr.", h).as_str()) + } else { + format!("{}:{}", "0.0.0.0", port.to_string()) + .parse::() + .unwrap() + }; + warp::serve(routes).run(addr).await; Ok(()) }