From beb039aa3cf634b0d55007491a739f8a45f291f7 Mon Sep 17 00:00:00 2001 From: finga Date: Sun, 7 Nov 2021 16:43:10 +0100 Subject: [PATCH] Reorder code To improve readibility and to prepeare split of `webhookey` code from `Rocket` some functions are reordered. --- src/main.rs | 134 ++++++++++++++++++++++++++-------------------------- 1 file changed, 67 insertions(+), 67 deletions(-) diff --git a/src/main.rs b/src/main.rs index eb00efa..8fba053 100644 --- a/src/main.rs +++ b/src/main.rs @@ -305,39 +305,6 @@ impl Hooks { } } -#[rocket::async_trait] -impl<'r> FromData<'r> for Hooks { - type Error = WebhookeyError; - - async fn from_data( - request: &'r Request<'_>, - data: Data<'r>, - ) -> Outcome> { - match Hooks::get_commands(request, data).await { - Ok(hooks) => { - if hooks.inner.is_empty() { - let client_ip = &request - .client_ip() - .unwrap_or(IpAddr::V4(Ipv4Addr::UNSPECIFIED)); - - warn!("Unmatched hook from {}", &client_ip); - return Failure((Status::NotFound, WebhookeyError::UnmatchedHook(*client_ip))); - } - - Success(hooks) - } - Err(WebhookeyError::Unauthorized(e)) => { - error!("{}", WebhookeyError::Unauthorized(e)); - Failure((Status::Unauthorized, WebhookeyError::Unauthorized(e))) - } - Err(e) => { - error!("{}", e); - Failure((Status::BadRequest, e)) - } - } - } -} - fn accept_ip(hook_name: &str, client_ip: &IpAddr, ip: &IpFilter) -> bool { if ip.validate(client_ip) { info!("Allow hook `{}` from {}", &hook_name, &client_ip); @@ -376,6 +343,18 @@ fn get_value_from_pointer<'a>(data: &'a serde_json::Value, pointer: &'a str) -> .ok_or_else(|| anyhow!("Could not convert value `{}` to string", value)) } +fn get_string(value: &serde_json::Value) -> Result { + match &value { + 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!() + } + } +} + fn replace_parameters( input: &str, headers: &HeaderMap, @@ -405,40 +384,6 @@ fn replace_parameters( Ok(result.join("")) } -fn get_string(value: &serde_json::Value) -> Result { - match &value { - 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!() - } - } -} - -#[post("/", format = "json", data = "")] -async fn receive_hook<'a>(address: SocketAddr, hooks: Hooks) -> Status { - info!("Post request received from: {}", address); - - hooks.inner.iter().for_each(|(name, command)| { - info!("Execute `{}` from hook `{}`", &command, &name); - - match run_script::run(command, &vec![], &ScriptOptions::new()) { - Ok((status, stdout, stderr)) => { - info!("Command `{}` exited with return code: {}", &command, status); - trace!("Output of command `{}` on stdout: {:?}", &command, &stdout); - debug!("Output of command `{}` on stderr: {:?}", &command, &stderr); - } - Err(e) => { - error!("Execution of `{}` failed: {}", &command, e); - } - } - }); - - Status::Ok -} - fn get_config() -> Result { // Look for config in CWD.. if let Ok(config) = File::open("config.yml") { @@ -472,6 +417,61 @@ fn get_config() -> Result { bail!("No configuration file found."); } +#[rocket::async_trait] +impl<'r> FromData<'r> for Hooks { + type Error = WebhookeyError; + + async fn from_data( + request: &'r Request<'_>, + data: Data<'r>, + ) -> Outcome> { + match Hooks::get_commands(request, data).await { + Ok(hooks) => { + if hooks.inner.is_empty() { + let client_ip = &request + .client_ip() + .unwrap_or(IpAddr::V4(Ipv4Addr::UNSPECIFIED)); + + warn!("Unmatched hook from {}", &client_ip); + return Failure((Status::NotFound, WebhookeyError::UnmatchedHook(*client_ip))); + } + + Success(hooks) + } + Err(WebhookeyError::Unauthorized(e)) => { + error!("{}", WebhookeyError::Unauthorized(e)); + Failure((Status::Unauthorized, WebhookeyError::Unauthorized(e))) + } + Err(e) => { + error!("{}", e); + Failure((Status::BadRequest, e)) + } + } + } +} + +#[post("/", format = "json", data = "")] +async fn receive_hook<'a>(address: SocketAddr, hooks: Hooks) -> Status { + info!("Post request received from: {}", address); + + hooks.inner.iter().for_each(|(name, command)| { + info!("Execute `{}` from hook `{}`", &command, &name); + + match run_script::run(command, &vec![], &ScriptOptions::new()) { + Ok((status, stdout, stderr)) => { + info!("Command `{}` exited with return code: {}", &command, status); + trace!("Output of command `{}` on stdout: {:?}", &command, &stdout); + debug!("Output of command `{}` on stderr: {:?}", &command, &stderr); + } + Err(e) => { + error!("Execution of `{}` failed: {}", &command, e); + } + } + }); + + Status::Ok +} + #[rocket::main] async fn main() -> Result<()> { env_logger::init();