Move config related code into config.rs
To continue with code separation to improve readability.
This commit is contained in:
parent
5e1d433c38
commit
1280352f25
2 changed files with 57 additions and 49 deletions
52
src/config.rs
Normal file
52
src/config.rs
Normal file
|
@ -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<IpFilter>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
|
#[serde(deny_unknown_fields)]
|
||||||
|
pub struct Config {
|
||||||
|
pub metrics: Option<MetricsConfig>,
|
||||||
|
pub hooks: BTreeMap<String, Hook>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_config() -> Result<File> {
|
||||||
|
// 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("<path unprintable>"),
|
||||||
|
);
|
||||||
|
|
||||||
|
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.");
|
||||||
|
}
|
54
src/main.rs
54
src/main.rs
|
@ -1,9 +1,11 @@
|
||||||
mod cli;
|
mod cli;
|
||||||
|
mod config;
|
||||||
mod filters;
|
mod filters;
|
||||||
mod metrics;
|
mod metrics;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
cli::Opts,
|
cli::Opts,
|
||||||
|
config::Config,
|
||||||
filters::{FilterType, IpFilter},
|
filters::{FilterType, IpFilter},
|
||||||
metrics::Metrics,
|
metrics::Metrics,
|
||||||
};
|
};
|
||||||
|
@ -50,21 +52,7 @@ pub enum WebhookeyError {
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
#[serde(deny_unknown_fields)]
|
#[serde(deny_unknown_fields)]
|
||||||
struct MetricsConfig {
|
pub struct Hook {
|
||||||
enabled: bool,
|
|
||||||
ip_filter: Option<IpFilter>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
|
||||||
#[serde(deny_unknown_fields)]
|
|
||||||
pub struct Config {
|
|
||||||
metrics: Option<MetricsConfig>,
|
|
||||||
hooks: BTreeMap<String, Hook>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
|
||||||
#[serde(deny_unknown_fields)]
|
|
||||||
struct Hook {
|
|
||||||
command: String,
|
command: String,
|
||||||
signature: String,
|
signature: String,
|
||||||
ip_filter: Option<IpFilter>,
|
ip_filter: Option<IpFilter>,
|
||||||
|
@ -266,39 +254,6 @@ pub fn get_string(data: &serde_json::Value) -> Result<String, WebhookeyError> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_config() -> Result<File> {
|
|
||||||
// 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("<path unprintable>"),
|
|
||||||
);
|
|
||||||
|
|
||||||
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]
|
#[rocket::async_trait]
|
||||||
impl<'r> FromData<'r> for Hooks {
|
impl<'r> FromData<'r> for Hooks {
|
||||||
type Error = WebhookeyError;
|
type Error = WebhookeyError;
|
||||||
|
@ -405,7 +360,7 @@ async fn main() -> Result<()> {
|
||||||
|
|
||||||
let config: Config = match cli.config {
|
let config: Config = match cli.config {
|
||||||
Some(config) => serde_yaml::from_reader(BufReader::new(File::open(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)?);
|
trace!("Parsed configuration:\n{}", serde_yaml::to_string(&config)?);
|
||||||
|
@ -429,6 +384,7 @@ async fn main() -> Result<()> {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::config::MetricsConfig;
|
||||||
use filters::{AddrType, HeaderFilter, JsonFilter};
|
use filters::{AddrType, HeaderFilter, JsonFilter};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use rocket::{
|
use rocket::{
|
||||||
|
|
Loading…
Reference in a new issue