chore: code cleanup
This commit is contained in:
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).
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
### Changed
|
||||||
|
- code cleanup
|
||||||
|
|
||||||
## [v0.4.0] - 2021-04-23
|
## [v0.4.0] - 2021-04-23
|
||||||
### Changed
|
### Changed
|
||||||
|
@ -29,7 +29,7 @@ pub(crate) struct Config {
|
|||||||
pub(crate) hosts: HashMap<std::net::IpAddr, u64>,
|
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 =>
|
clap_app!(myapp =>
|
||||||
(name: crate_name!())
|
(name: crate_name!())
|
||||||
(version: crate_version!())
|
(version: crate_version!())
|
||||||
@ -42,7 +42,7 @@ pub(crate) fn setup_clap() -> clap::ArgMatches<'static> {
|
|||||||
.get_matches()
|
.get_matches()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn setup_fern(level: u64) {
|
fn setup_fern(level: u64) {
|
||||||
let level = match level {
|
let level = match level {
|
||||||
0 => log::LevelFilter::Error,
|
0 => log::LevelFilter::Error,
|
||||||
1 => log::LevelFilter::Warn,
|
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")?;
|
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")?)
|
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!")?)
|
||||||
|
}
|
||||||
|
18
src/main.rs
18
src/main.rs
@ -17,25 +17,23 @@
|
|||||||
* You should have received a copy of the GNU Affero General Public License *
|
* 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/>. *
|
* 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 config;
|
||||||
mod metrics;
|
mod metrics;
|
||||||
mod ping;
|
mod ping;
|
||||||
|
|
||||||
use crate::config::{read_config, setup_clap, setup_fern};
|
use config::setup_app;
|
||||||
use crate::metrics::start_serving_metrics;
|
use metrics::start_serving_metrics;
|
||||||
use crate::ping::start_pinging_hosts;
|
use ping::start_pinging_hosts;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
let clap = setup_clap();
|
let config = setup_app()?;
|
||||||
setup_fern(clap.occurrences_of("v"));
|
|
||||||
let config =
|
|
||||||
read_config(clap.value_of("config").unwrap()).context("Couldn't read config file!")?;
|
|
||||||
|
|
||||||
let ping_fut = start_pinging_hosts(config.clone());
|
let ping_fut = catch(start_pinging_hosts(&config));
|
||||||
let serve_fut = start_serving_metrics(config.clone());
|
let serve_fut = catch(start_serving_metrics(&config));
|
||||||
|
|
||||||
futures::pin_mut!(ping_fut);
|
futures::pin_mut!(ping_fut);
|
||||||
futures::pin_mut!(serve_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")?)
|
.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 {
|
let serve_future = Server::bind(&config.listener).serve(make_service_fn(|_| async {
|
||||||
Ok::<_, hyper::Error>(service_fn(serve_req))
|
Ok::<_, hyper::Error>(service_fn(serve_req))
|
||||||
}));
|
}));
|
||||||
|
@ -41,15 +41,15 @@ lazy_static! {
|
|||||||
.unwrap();
|
.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 pinger = Pinger::new().await.context("Couldn't create pinger")?;
|
||||||
let mut handles = vec![];
|
let mut handles = vec![];
|
||||||
for (host, interval) in config.hosts.clone() {
|
for (host, interval) in config.hosts.clone() {
|
||||||
info!("Spawn ping task for {}", host);
|
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;
|
let (result, _, _) = futures::future::select_all(handles).await;
|
||||||
result?;
|
result??;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user