Implement proper logging

The `log` and `env_logger` crates are used for logging.
This commit is contained in:
finga 2021-03-03 16:14:54 +01:00
parent c8505b27c5
commit 82ccbf0a7e
3 changed files with 59 additions and 17 deletions

View file

@ -1,15 +1,13 @@
#![feature(proc_macro_hygiene, decl_macro)]
use anyhow::{anyhow, bail, Result};
use log::{debug, info, trace};
use regex::Regex;
use rocket::{fairing::AdHoc, get, post, routes, State};
use rocket_contrib::json::Json;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs::File;
use std::io::BufReader;
use std::process::Command;
use std::{collections::HashMap, fs::File, io::BufReader, net::SocketAddr, process::Command};
#[derive(Debug, Deserialize, Serialize)]
struct Config {
@ -37,23 +35,20 @@ fn index() -> &'static str {
}
fn execute_hook(name: &str, hook: &Hook, data: &serde_json::Value) -> Result<()> {
println!(
"hook: {:?} action: {:?} filters: {:?}",
name, hook.action, hook.filters
);
debug!("Running hook `{}`", name);
for (filter_name, filter) in hook.filters.iter() {
println!("filter: {:?}", filter);
debug!("Matching filter `{}`", filter_name);
if let Some(value) = data.pointer(&filter.pointer) {
let regex = Regex::new(&filter.regex)?;
if let Some(value) = value.as_str() {
if !regex.is_match(value) {
info!("Filter `{}` in hook `{}` did not match", filter_name, name);
return Ok(());
}
} else {
println!("Error could not parse pointer");
anyhow!(
"Could not parse pointer in hook `{}` from filter `{}`",
name,
@ -63,10 +58,8 @@ fn execute_hook(name: &str, hook: &Hook, data: &serde_json::Value) -> Result<()>
}
}
println!("execute action");
if let Some(action) = &hook.action {
println!("execute {}", action);
info!("Execute `{}` from hook `{}`", action, name);
Command::new(action).spawn()?;
}
@ -75,9 +68,13 @@ fn execute_hook(name: &str, hook: &Hook, data: &serde_json::Value) -> Result<()>
}
#[post("/", format = "json", data = "<data>")]
fn receive_hook(config: State<Config>, data: Json<Data>) -> Result<String> {
fn receive_hook(address: SocketAddr, config: State<Config>, data: Json<Data>) -> Result<String> {
info!("POST request received from: {}", address);
let data = serde_json::to_value(data.0)?;
trace!("Data received from: {}\n{}", address, data);
for (hook_name, hook) in config.hooks.iter() {
execute_hook(&hook_name, &hook, &data)?;
}
@ -87,18 +84,27 @@ fn receive_hook(config: State<Config>, data: Json<Data>) -> Result<String> {
fn get_config() -> Result<File> {
if let Ok(config) = File::open("/etc/webhookey/config.yml") {
info!("Loading configuration from `/etc/webhookey/config.yml`");
return Ok(config);
}
if let Some(mut path) = dirs::config_dir() {
path.push("webhookey/config.yml");
if let Ok(config) = File::open(path) {
if let Ok(config) = File::open(&path) {
info!(
"Loading configuration from `{}`",
path.to_str().unwrap_or("path not printable"),
);
return Ok(config);
}
}
if let Ok(config) = File::open("config.yml") {
info!("Loading configuration from `./config.yml`");
return Ok(config);
}
@ -106,9 +112,13 @@ fn get_config() -> Result<File> {
}
fn main() -> Result<()> {
let config: Config = serde_yaml::from_reader(BufReader::new(get_config()?))?;
env_logger::init();
println!("{}", serde_yaml::to_string(&config)?);
let config = get_config()?;
let config: Config = serde_yaml::from_reader(BufReader::new(config))?;
trace!("Parsed configuration:\n{}", serde_yaml::to_string(&config)?);
rocket::ignite()
.mount("/", routes![index, receive_hook])