iwll figure out owning ref soon

This commit is contained in:
Michael 2018-08-17 02:04:52 +00:00
parent 39a60b7d30
commit 5ad14efdaf
6 changed files with 43 additions and 13 deletions

16
Cargo.lock generated
View file

@ -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"

View file

@ -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"

View file

@ -59,9 +59,9 @@ impl Handler {
pub fn run(
&self,
temp_path: &PathBuf,
temp_path: PathBuf,
input: JsonValue,
) -> impl Future<Item = JsonValue, Error = Error> {
) -> impl Future<Item = (PathBuf, JsonValue), Error = Error> {
let config = {
let mut buf: Vec<u8> = 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,
}))
}),
))
}
}

View file

@ -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<Handler>,
handlers: Arc<Vec<Handler>>,
}
impl Hook {
pub fn from(name: impl Into<String>, config: &Value) -> Result<Self, Error> {
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::<Result<Vec<_>, _>>()?;
.collect::<Result<Vec<_>, _>>()?);
Ok(Hook { name, handlers })
}
pub fn from_file<P>(path: P) -> Result<Hook, Error>
@ -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<String, String> {
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<String, String> {
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()

View file

@ -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]

View file

@ -60,7 +60,7 @@ pub fn dip_service(req: Request<Body>) -> Box<Future<Item = Response<Body>, 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),
};