Skip to content
This repository has been archived by the owner on May 10, 2023. It is now read-only.

Commit

Permalink
feat: properly add host support, fix env support
Browse files Browse the repository at this point in the history
  • Loading branch information
auguwu committed Apr 5, 2022
1 parent b1f1aa4 commit a89aafe
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

[package]
name = "sentry-worker"
version = "0.1.1"
version = "0.1.2"
edition = "2021"
authors = ["Noel <[email protected]>"]

Expand Down
15 changes: 12 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ fn env_to_config() -> Config {
sentry_dsn,
port: if port.is_some() {
let p = port.unwrap();
Some(p.parse::<u16>().unwrap())
Some(
p.parse::<u16>()
.expect("Unable to parse String -> u16 for http port."),
)
} else {
None
},
Expand All @@ -53,6 +56,12 @@ fn env_to_config() -> Config {
}

pub static CONFIG: Lazy<Config> = 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::<Config>(&contents).expect("Unable to parse 'config.toml' contents.")
}
Err(_) => env_to_config(),
}
});
19 changes: 14 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,15 +41,24 @@ async fn main() -> Result<()> {
.and(warp::body::json::<HashMap<String, serde_json::Value>>())
.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::<SocketAddr>()
.expect(format!("Unable to parse host '{}' -> SocketAddr.", h).as_str())
} else {
format!("{}:{}", "0.0.0.0", port.to_string())
.parse::<SocketAddr>()
.unwrap()
};

warp::serve(routes).run(addr).await;
Ok(())
}

Expand Down

0 comments on commit a89aafe

Please sign in to comment.