diff --git a/Cargo.lock b/Cargo.lock index 18b5940..15d06a9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -194,6 +194,7 @@ dependencies = [ "lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "mktemp 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "notify 4.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "secstr 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.71 (registry+https://github.com/rust-lang/crates.io-index)", @@ -594,6 +595,14 @@ dependencies = [ "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "owning_ref" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "proc-macro2" version = "0.4.11" @@ -777,6 +786,11 @@ dependencies = [ "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "stable_deref_trait" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "string" version = "0.1.1" @@ -1205,6 +1219,7 @@ dependencies = [ "checksum nodrop 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "9a2228dca57108069a5262f2ed8bd2e82496d2e074a06d1ccc7ce1687b6ae0a2" "checksum notify 4.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d023ef40ca7680784b07be3f49913e1ea176da1b63949f2eb2fed96438bd7f42" "checksum num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c51a3322e4bca9d212ad9a158a02abc6934d005490c054a2778df73a70aa0a30" +"checksum owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cdf84f41639e037b484f93433aa3897863b561ed65c6e59c7073d7c561710f37" "checksum proc-macro2 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)" = "762eea716b821300a86da08870a64b597304866ceb9f54a11d67b4cf56459c6a" "checksum quote 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ed7d650913520df631972f21e104a4fa2f9c82a14afc65d17b388a2e29731e7c" "checksum rand 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)" = "15a732abf9d20f0ad8eeb6f909bf6868722d9a06e1e50802b6a70351f40b4eb1" @@ -1229,6 +1244,7 @@ dependencies = [ "checksum slab 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d807fd58c4181bbabed77cb3b891ba9748241a552bcc5be698faaebefc54f46e" "checksum slab 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5f9776d6b986f77b35c6cf846c11ad986ff128fe0b2b63a3628e3755e8d3102d" "checksum socket2 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "962a516af4d3a7c272cb3a1d50a8cc4e5b41802e4ad54cfb7bee8ba61d37d703" +"checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8" "checksum string 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "00caf261d6f90f588f8450b8e1230fa0d5be49ee6140fdfbcb55335aff350970" "checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550" "checksum structopt 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "d8e9ad6a11096cbecdcca0cc6aa403fdfdbaeda2fb3323a39c98e6a166a1e45a" diff --git a/Cargo.toml b/Cargo.toml index 06b6648..f0b6e5c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ hyper = "0.12" mktemp = "0.3" lazy_static = "1.1" notify = "4.0" +owning_ref = "0.3" regex = "1.0" serde = "1.0" serde_derive = "1.0" diff --git a/src/handler.rs b/src/handler.rs index 777dec9..bcd41be 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -59,9 +59,9 @@ impl Handler { pub fn run( &self, - temp_path: &PathBuf, + temp_path: PathBuf, input: JsonValue, - ) -> impl Future { + ) -> impl Future { let config = { let mut buf: Vec = Vec::new(); { @@ -135,9 +135,12 @@ impl Handler { let stdout = String::from_utf8(output.stdout).unwrap_or_else(|_| String::new()); let stderr = String::from_utf8(output.stderr).unwrap_or_else(|_| String::new()); - future::ok(json!({ + future::ok(( + temp_path, + json!({ "stdout": stdout, "stderr": stderr, - })) + }), + )) } } diff --git a/src/hook.rs b/src/hook.rs index 186bb7d..4158349 100644 --- a/src/hook.rs +++ b/src/hook.rs @@ -2,9 +2,11 @@ use std::fs::File; use std::io::Read; use std::path::{Path, PathBuf}; use std::slice::Iter; +use std::sync::Arc; use failure::{err_msg, Error}; -use futures::{future, Future}; +use futures::{future, stream, Future}; +use owning_ref::BoxRef; use serde_json::Value as JsonValue; use tokio::{self, prelude::*}; use toml::Value; @@ -13,20 +15,20 @@ use Handler; pub struct Hook { name: String, - handlers: Vec, + handlers: Arc>, } impl Hook { pub fn from(name: impl Into, config: &Value) -> Result { let name = name.into(); - let handlers = config + let handlers = Arc::new(config .get("handlers") .ok_or(err_msg("No 'handlers' found."))? .as_array() .ok_or(err_msg("'handlers' is not an array."))? .iter() .map(|value: &Value| Handler::from(value)) - .collect::, _>>()?; + .collect::, _>>()?); Ok(Hook { name, handlers }) } pub fn from_file

(path: P) -> Result @@ -50,11 +52,18 @@ impl Hook { pub fn get_name(&self) -> String { self.name.clone() } - pub fn handle(&self, req: JsonValue, temp_path: &PathBuf) -> Result { - let fut = self.handlers.iter().fold(future::ok(req), |prev, handler| { - prev.and_then(|val| handler.run(temp_path, val)) + pub fn handle(&self, req: JsonValue, temp_path: PathBuf) -> Result { + let h = self.handlers.clone(); + let it = h.iter(); + let s = stream::iter_ok(it).fold((temp_path, req), move |prev, handler| { + let (path, prev) = prev; + handler.run(path, prev) }); - tokio::executor::spawn(fut.and_then(|_| future::ok(()))); + /*.fold(future::ok(req), |prev, handler| { + prev.and_then(|val| handler.run(temp_path, val)) + });*/ + let s = s.map(|_| ()).map_err(|_: Error| ()); + tokio::executor::spawn(s); Ok("success".to_owned()) /* Ok(self.iter() diff --git a/src/lib.rs b/src/lib.rs index b782436..c60aa99 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,7 @@ extern crate failure; extern crate futures; extern crate hyper; extern crate mktemp; +extern crate owning_ref; extern crate serde; extern crate tokio; #[macro_use] diff --git a/src/service.rs b/src/service.rs index 30df045..9e60c8e 100644 --- a/src/service.rs +++ b/src/service.rs @@ -60,7 +60,7 @@ pub fn dip_service(req: Request) -> Box, Erro assert!(temp_path.exists()); let hook = hooks.get(&name).unwrap(); - let (code, msg) = match hook.handle(req_obj, &temp_path) { + let (code, msg) = match hook.handle(req_obj, temp_path) { Ok(msg) => (StatusCode::ACCEPTED, msg), Err(msg) => (StatusCode::BAD_REQUEST, msg), };