diff --git a/Cargo.lock b/Cargo.lock index 7c41c95..18b5940 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -201,6 +201,8 @@ dependencies = [ "serde_json 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)", "sha-1 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "structopt 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-process 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "walkdir 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -483,6 +485,27 @@ dependencies = [ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "mio-named-pipes" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "log 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.15 (registry+https://github.com/rust-lang/crates.io-index)", + "miow 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "mio-uds" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.15 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "miow" version = "0.1.5" @@ -505,6 +528,15 @@ dependencies = [ "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "miow" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "socket2 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "mktemp" version = "0.3.1" @@ -734,6 +766,17 @@ name = "slab" version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "socket2" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "string" version = "0.1.1" @@ -876,6 +919,21 @@ dependencies = [ "log 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "tokio-process" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "futures 0.1.23 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.15 (registry+https://github.com/rust-lang/crates.io-index)", + "mio-named-pipes 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-io 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-reactor 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-signal 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "tokio-reactor" version = "0.1.3" @@ -889,6 +947,21 @@ dependencies = [ "tokio-io 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "tokio-signal" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "futures 0.1.23 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.15 (registry+https://github.com/rust-lang/crates.io-index)", + "mio-uds 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-executor 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-io 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-reactor 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "tokio-tcp" version = "0.1.1" @@ -1121,8 +1194,11 @@ dependencies = [ "checksum memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0f9dc261e2b62d7a622bf416ea3c5245cdd5d9a7fcc428c0d06804dfce1775b3" "checksum mio 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a637d1ca14eacae06296a008fa7ad955347e34efcb5891cfd8ba05491a37907e" "checksum mio 0.6.15 (registry+https://github.com/rust-lang/crates.io-index)" = "4fcfcb32d63961fb6f367bfd5d21e4600b92cd310f71f9dca25acae196eb1560" +"checksum mio-named-pipes 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "f5e374eff525ce1c5b7687c4cef63943e7686524a387933ad27ca7ec43779cb3" +"checksum mio-uds 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)" = "84c7b5caa3a118a6e34dbac36504503b1e8dc5835e833306b9d6af0e05929f79" "checksum miow 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "3e690c5df6b2f60acd45d56378981e827ff8295562fc8d34f573deb267a59cd1" "checksum miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919" +"checksum miow 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9224c91f82b3c47cf53dcf78dfaa20d6888fbcc5d272d5f2fcdf8a697f3c987d" "checksum mktemp 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "77001ceb9eed65439f3dc2a2543f9ba1417d912686bf224a7738d0966e6dcd69" "checksum net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "42550d9fb7b6684a6d404d9fa7250c2eb2646df731d1c06afc06dcee9e1bcf88" "checksum nix 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "bfb3ddedaa14746434a02041940495bf11325c22f6d36125d3bdd56090d50a79" @@ -1152,6 +1228,7 @@ dependencies = [ "checksum sha-1 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "51b9d1f3b5de8a167ab06834a7c883bd197f2191e1dda1a22d9ccfeedbf9aded" "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 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" @@ -1167,7 +1244,9 @@ dependencies = [ "checksum tokio-executor 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "424f0c87ecd66b863045d84e384cb7ce0ae384d8b065b9f0363d29c0d1b30b2f" "checksum tokio-fs 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "b5cbe4ca6e71cb0b62a66e4e6f53a8c06a6eefe46cc5f665ad6f274c9906f135" "checksum tokio-io 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "a5c9635ee806f26d302b8baa1e145689a280d8f5aa8d0552e7344808da54cc21" +"checksum tokio-process 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0832648d1ff7ca42c06ca45dc76797b92c56500de828e33c77276fa1449947b6" "checksum tokio-reactor 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8703a5762ff6913510dc64272c714c4389ffd8c4b3cf602879b8bd14ff06b604" +"checksum tokio-signal 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "52ba234f769af7a85b0cf579da820a82675940054b99819beba2a01a356f5f8e" "checksum tokio-tcp 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5b4c329b47f071eb8a746040465fa751bd95e4716e98daef6a9b4e434c17d565" "checksum tokio-threadpool 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "24ab84f574027b0e875378f31575cf175360891919e93a3490f07e76e00e4efb" "checksum tokio-timer 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "1c76b4e97a4f61030edff8bd272364e4f731b9f54c7307eb4eb733c3926eb96a" diff --git a/Cargo.toml b/Cargo.toml index b0c4e6d..06b6648 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,8 @@ serde = "1.0" serde_derive = "1.0" serde_json = "1.0" structopt = "0.2" +tokio = "0.1" +tokio-process = "0.2" toml = "0.4" walkdir = "2.2" diff --git a/src/handler.rs b/src/handler.rs index b96035a..777dec9 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -3,6 +3,7 @@ use std::path::PathBuf; use std::process::{Command, Stdio}; use failure::{err_msg, Error}; +use futures::{future, Future}; use serde::Serialize; use serde_json::{Serializer as JsonSerializer, Value as JsonValue}; use toml::Value as TomlValue; @@ -56,12 +57,16 @@ impl Handler { Ok(Handler { config, action }) } - pub fn run(&self, temp_path: &PathBuf, input: JsonValue) -> Result { + pub fn run( + &self, + temp_path: &PathBuf, + input: JsonValue, + ) -> impl Future { let config = { let mut buf: Vec = Vec::new(); { let mut serializer = JsonSerializer::new(&mut buf); - TomlValue::serialize(&self.config, &mut serializer)?; + TomlValue::serialize(&self.config, &mut serializer); } String::from_utf8(buf).unwrap() }; @@ -78,11 +83,12 @@ impl Handler { .stdin(Stdio::piped()) .stdout(Stdio::piped()) .stderr(Stdio::piped()) - .spawn()?; - let output = child.wait_with_output()?; + .spawn() + .unwrap(); + let output = child.wait_with_output().unwrap(); if !output.status.success() { // TODO: get rid of unwraps - return Err(err_msg(format!( + return future::err(err_msg(format!( "Command '{}' returned with a non-zero status code: {}\nstdout:\n{}\nstderr:\n{}", cmd, output.status, @@ -102,19 +108,20 @@ impl Handler { .stdin(Stdio::piped()) .stdout(Stdio::piped()) .stderr(Stdio::piped()) - .spawn()?; + .spawn() + .unwrap(); { match child.stdin { Some(ref mut stdin) => { - write!(stdin, "{}", input)?; + write!(stdin, "{}", input); } - None => bail!("done fucked"), + None => return future::err(err_msg("done fucked")), }; } - let output = child.wait_with_output()?; + let output = child.wait_with_output().unwrap(); if !output.status.success() { // TODO: get rid of unwraps - return Err(err_msg(format!( + return future::err(err_msg(format!( "'{:?}' returned with a non-zero status code: {}\nstdout:\n{}\nstderr:\n{}", path, output.status, @@ -128,9 +135,9 @@ 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()); - Ok(json!({ - "stdout": stdout, - "stderr": stderr, - })) + future::ok(json!({ + "stdout": stdout, + "stderr": stderr, + })) } } diff --git a/src/hook.rs b/src/hook.rs index 15673fb..186bb7d 100644 --- a/src/hook.rs +++ b/src/hook.rs @@ -4,8 +4,9 @@ use std::path::{Path, PathBuf}; use std::slice::Iter; use failure::{err_msg, Error}; -use hyper::StatusCode; +use futures::{future, Future}; use serde_json::Value as JsonValue; +use tokio::{self, prelude::*}; use toml::Value; use Handler; @@ -49,20 +50,18 @@ impl Hook { pub fn get_name(&self) -> String { self.name.clone() } - pub fn iter(&self) -> Iter { - self.handlers.iter() - } - pub fn handle( - &self, - req: JsonValue, - temp_path: &PathBuf, - ) -> Result<(StatusCode, String), Error> { + 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)) + }); + tokio::executor::spawn(fut.and_then(|_| future::ok(()))); + Ok("success".to_owned()) + /* Ok(self.iter() .fold(Ok(req), |prev, handler| { prev.and_then(|val| { println!("Running {}...", handler.config()); let result = handler.run(&temp_path, val); - println!("{:?}", result); result }) }) @@ -77,5 +76,6 @@ impl Hook { ) }) .unwrap_or_else(|err| (StatusCode::BAD_REQUEST, format!("Error: {:?}", err)))) + */ } } diff --git a/src/lib.rs b/src/lib.rs index 047a6aa..b782436 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,7 @@ extern crate futures; extern crate hyper; extern crate mktemp; extern crate serde; +extern crate tokio; #[macro_use] extern crate serde_json; #[macro_use] diff --git a/src/service.rs b/src/service.rs index 8afaa00..30df045 100644 --- a/src/service.rs +++ b/src/service.rs @@ -60,7 +60,10 @@ pub fn dip_service(req: Request) -> Box, Erro assert!(temp_path.exists()); let hook = hooks.get(&name).unwrap(); - let (code, msg) = hook.handle(req_obj, &temp_path).unwrap(); + let (code, msg) = match hook.handle(req_obj, &temp_path) { + Ok(msg) => (StatusCode::ACCEPTED, msg), + Err(msg) => (StatusCode::BAD_REQUEST, msg), + }; temp_dir.release(); Response::builder()