chore: code cleanup

main
Jan Christian Grünhage 4 years ago
parent 0f3dc4ee21
commit a17ee534b2

@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
### Changed
- code cleanup
## [v0.4.0] - 2021-04-23
### Changed

@ -29,7 +29,7 @@ pub(crate) struct Config {
pub(crate) hosts: HashMap<std::net::IpAddr, u64>,
}
pub(crate) fn setup_clap() -> clap::ArgMatches<'static> {
fn setup_clap() -> clap::ArgMatches<'static> {
clap_app!(myapp =>
(name: crate_name!())
(version: crate_version!())
@ -42,7 +42,7 @@ pub(crate) fn setup_clap() -> clap::ArgMatches<'static> {
.get_matches()
}
pub(crate) fn setup_fern(level: u64) {
fn setup_fern(level: u64) {
let level = match level {
0 => log::LevelFilter::Error,
1 => log::LevelFilter::Warn,
@ -70,7 +70,16 @@ pub(crate) fn setup_fern(level: u64) {
}
}
pub(crate) fn read_config(path: &str) -> Result<Config> {
fn read_config(path: &str) -> Result<Config> {
let config_file_content = std::fs::read_to_string(path).context("Couldn't read config file")?;
Ok(toml::from_str(&config_file_content).context("Couldn't parse config file")?)
}
pub(crate) fn setup_app() -> Result<Config> {
let clap = setup_clap();
setup_fern(clap.occurrences_of("v"));
let config_path = clap
.value_of("config")
.context("Got no config file. clap should've catched this")?;
Ok(read_config(config_path).context("Couldn't read config file!")?)
}

@ -17,25 +17,23 @@
* You should have received a copy of the GNU Affero General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
********************************************************************************/
use anyhow::{Context, Result};
use anyhow::Result;
use async_anyhow_logger::catch;
mod config;
mod metrics;
mod ping;
use crate::config::{read_config, setup_clap, setup_fern};
use crate::metrics::start_serving_metrics;
use crate::ping::start_pinging_hosts;
use config::setup_app;
use metrics::start_serving_metrics;
use ping::start_pinging_hosts;
#[tokio::main]
async fn main() -> Result<()> {
let clap = setup_clap();
setup_fern(clap.occurrences_of("v"));
let config =
read_config(clap.value_of("config").unwrap()).context("Couldn't read config file!")?;
let config = setup_app()?;
let ping_fut = start_pinging_hosts(config.clone());
let serve_fut = start_serving_metrics(config.clone());
let ping_fut = catch(start_pinging_hosts(&config));
let serve_fut = catch(start_serving_metrics(&config));
futures::pin_mut!(ping_fut);
futures::pin_mut!(serve_fut);

@ -95,7 +95,7 @@ async fn serve_not_found() -> Result<Response<Body>> {
.context("Couldn't build not found response")?)
}
pub(crate) async fn start_serving_metrics(config: Config) -> Result<()> {
pub(crate) async fn start_serving_metrics(config: &Config) -> Result<()> {
let serve_future = Server::bind(&config.listener).serve(make_service_fn(|_| async {
Ok::<_, hyper::Error>(service_fn(serve_req))
}));

@ -41,15 +41,15 @@ lazy_static! {
.unwrap();
}
pub(crate) async fn start_pinging_hosts(config: Config) -> Result<()> {
pub(crate) async fn start_pinging_hosts(config: &Config) -> Result<()> {
let pinger = Pinger::new().await.context("Couldn't create pinger")?;
let mut handles = vec![];
for (host, interval) in config.hosts.clone() {
info!("Spawn ping task for {}", host);
handles.push(tokio::spawn(catch(ping_host(pinger.clone(), host, interval))));
handles.push(tokio::spawn(ping_host(pinger.clone(), host, interval)));
}
let (result, _, _) = futures::future::select_all(handles).await;
result?;
result??;
Ok(())
}

Loading…
Cancel
Save