71 lines
1.9 KiB
Rust
71 lines
1.9 KiB
Rust
mod cli;
|
|
mod config;
|
|
mod filters;
|
|
mod hooks;
|
|
mod metrics;
|
|
|
|
use crate::{cli::Opts, config::Config, metrics::Metrics};
|
|
use anyhow::Result;
|
|
use clap::Parser;
|
|
use log::{debug, error, trace};
|
|
use rocket::routes;
|
|
use std::{fs::File, io::BufReader, net::IpAddr};
|
|
use thiserror::Error;
|
|
|
|
#[derive(Debug, Error)]
|
|
pub enum WebhookeyError {
|
|
#[error("Could not extract signature from header")]
|
|
InvalidSignature,
|
|
#[error("Unauthorized request from `{0}`")]
|
|
Unauthorized(IpAddr),
|
|
#[error("Unmatched hook from `{0}`")]
|
|
UnmatchedHook(IpAddr),
|
|
#[error("Could not evaluate filter request")]
|
|
InvalidFilter,
|
|
#[error("IO Error")]
|
|
Io(#[from] std::io::Error),
|
|
#[error("Serde Error")]
|
|
Serde(#[from] serde_json::Error),
|
|
}
|
|
|
|
pub fn get_string(data: &serde_json::Value) -> Result<String, WebhookeyError> {
|
|
match &data {
|
|
serde_json::Value::Bool(bool) => Ok(bool.to_string()),
|
|
serde_json::Value::Number(number) => Ok(number.to_string()),
|
|
serde_json::Value::String(string) => Ok(string.as_str().to_string()),
|
|
x => {
|
|
error!("Could not get string from: {:?}", x);
|
|
unimplemented!()
|
|
}
|
|
}
|
|
}
|
|
|
|
#[rocket::main]
|
|
async fn main() -> Result<()> {
|
|
env_logger::init();
|
|
|
|
let cli: Opts = Opts::parse();
|
|
|
|
let config: Config = match cli.config {
|
|
Some(config) => serde_yaml::from_reader(BufReader::new(File::open(config)?))?,
|
|
_ => serde_yaml::from_reader(BufReader::new(config::get_config()?))?,
|
|
};
|
|
|
|
trace!("Parsed configuration:\n{}", serde_yaml::to_string(&config)?);
|
|
|
|
if cli.command.is_some() {
|
|
debug!("Configtest succeded.");
|
|
println!("Config is OK");
|
|
return Ok(());
|
|
}
|
|
|
|
rocket::build()
|
|
.mount("/", routes![hooks::receive_hook, metrics::metrics])
|
|
.manage(config)
|
|
.manage(Metrics::default())
|
|
.launch()
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|