diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..ee45a4f --- /dev/null +++ b/src/config.rs @@ -0,0 +1,52 @@ +use crate::{Hook, IpFilter}; +use anyhow::{bail, Result}; +use log::info; +use serde::{Deserialize, Serialize}; +use std::{collections::BTreeMap, fs::File}; + +#[derive(Debug, Deserialize, Serialize)] +#[serde(deny_unknown_fields)] +pub struct MetricsConfig { + pub enabled: bool, + pub ip_filter: Option, +} + +#[derive(Debug, Deserialize, Serialize)] +#[serde(deny_unknown_fields)] +pub struct Config { + pub metrics: Option, + pub hooks: BTreeMap, +} + +pub fn get_config() -> Result { + // Look for config in CWD.. + if let Ok(config) = File::open("config.yml") { + info!("Loading configuration from `./config.yml`"); + + return Ok(config); + } + + // ..look for user path config.. + if let Some(mut path) = dirs::config_dir() { + path.push("webhookey/config.yml"); + + if let Ok(config) = File::open(&path) { + info!( + "Loading configuration from `{}`", + path.to_str().unwrap_or(""), + ); + + return Ok(config); + } + } + + // ..look for systemwide config.. + if let Ok(config) = File::open("/etc/webhookey/config.yml") { + info!("Loading configuration from `/etc/webhookey/config.yml`"); + + return Ok(config); + } + + // ..you had your chance. + bail!("No configuration file found."); +} diff --git a/src/main.rs b/src/main.rs index a7850e3..d97f601 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,11 @@ mod cli; +mod config; mod filters; mod metrics; use crate::{ cli::Opts, + config::Config, filters::{FilterType, IpFilter}, metrics::Metrics, }; @@ -50,21 +52,7 @@ pub enum WebhookeyError { #[derive(Debug, Deserialize, Serialize)] #[serde(deny_unknown_fields)] -struct MetricsConfig { - enabled: bool, - ip_filter: Option, -} - -#[derive(Debug, Deserialize, Serialize)] -#[serde(deny_unknown_fields)] -pub struct Config { - metrics: Option, - hooks: BTreeMap, -} - -#[derive(Debug, Deserialize, Serialize)] -#[serde(deny_unknown_fields)] -struct Hook { +pub struct Hook { command: String, signature: String, ip_filter: Option, @@ -266,39 +254,6 @@ pub fn get_string(data: &serde_json::Value) -> Result { } } -fn get_config() -> Result { - // Look for config in CWD.. - if let Ok(config) = File::open("config.yml") { - info!("Loading configuration from `./config.yml`"); - - return Ok(config); - } - - // ..look for user path config.. - if let Some(mut path) = dirs::config_dir() { - path.push("webhookey/config.yml"); - - if let Ok(config) = File::open(&path) { - info!( - "Loading configuration from `{}`", - path.to_str().unwrap_or(""), - ); - - return Ok(config); - } - } - - // ..look for systemwide config.. - if let Ok(config) = File::open("/etc/webhookey/config.yml") { - info!("Loading configuration from `/etc/webhookey/config.yml`"); - - return Ok(config); - } - - // ..you had your chance. - bail!("No configuration file found."); -} - #[rocket::async_trait] impl<'r> FromData<'r> for Hooks { type Error = WebhookeyError; @@ -405,7 +360,7 @@ async fn main() -> Result<()> { let config: Config = match cli.config { Some(config) => serde_yaml::from_reader(BufReader::new(File::open(config)?))?, - _ => serde_yaml::from_reader(BufReader::new(get_config()?))?, + _ => serde_yaml::from_reader(BufReader::new(config::get_config()?))?, }; trace!("Parsed configuration:\n{}", serde_yaml::to_string(&config)?); @@ -429,6 +384,7 @@ async fn main() -> Result<()> { #[cfg(test)] mod tests { use super::*; + use crate::config::MetricsConfig; use filters::{AddrType, HeaderFilter, JsonFilter}; use regex::Regex; use rocket::{