diff --git a/examples/github.rs b/examples/github.rs index 8be1917..2574d9f 100644 --- a/examples/github.rs +++ b/examples/github.rs @@ -36,6 +36,8 @@ struct Config { secret: String, #[serde(default)] disable_hmac_verify: bool, + #[serde(default = "default_path")] + path: PathBuf, } #[derive(Serialize, Deserialize)] @@ -54,6 +56,10 @@ struct GithubPayload { repository: RepositoryInfo, } +fn default_path() -> PathBuf { + PathBuf::from(".") +} + fn main() -> Result<(), Error> { let args = Opt::from_args(); let config: Config = serde_json::from_str(&args.config)?; @@ -86,8 +92,7 @@ fn main() -> Result<(), Error> { let payload: GithubPayload = serde_json::from_str(&payload.body)?; let mut target_path = PathBuf::from(env::var("DIP_WORKDIR")?); - target_path.push("repository"); - println!("{:?}", &target_path); + target_path.push(&config.path); Command::new("git") .arg("clone") .arg(&payload.repository.clone_url) diff --git a/src/handler.rs b/src/handler.rs index 4ef61e8..f63b040 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -20,6 +20,9 @@ pub enum Action { } impl Handler { + pub fn config(&self) -> &TomlValue { + &self.config + } pub fn from(config: &TomlValue) -> Result { let handler = config .get("type") diff --git a/src/service.rs b/src/service.rs index 47d36d7..70891f6 100644 --- a/src/service.rs +++ b/src/service.rs @@ -1,4 +1,5 @@ use std::collections::HashMap; +use std::thread; use futures::{future, Future, Stream}; use hyper::{Body, Error, Request, Response, StatusCode}; @@ -42,56 +43,58 @@ pub fn dip_service(req: Request) -> Box, Erro }) .collect::>(); let method = req.method().as_str().to_owned(); - // probably not idiomatically the best way to do it - // i was just trying to get something working - let body = req.into_body(); - Box::new(body.concat2().map(move |body| { - let req_obj = json!({ - "body": String::from_utf8(body.to_vec()).unwrap(), - "headers": headers, - "method": method, - }); - let hooks = HOOKS.lock().unwrap(); - { - let mut temp_dir = Temp::new_dir().unwrap(); - let temp_path = temp_dir.to_path_buf(); - assert!(temp_path.exists()); - let hook = hooks.get(&name).unwrap(); - let (code, msg) = hook.iter() - .fold(Ok(req_obj), |prev, handler| { - prev.and_then(|val| handler.run(&temp_path, val)) - }) - .map(|res| { - ( - StatusCode::ACCEPTED, - format!( - "stdout:\n{}\n\nstderr:\n{}", - res.get("stdout").and_then(|v| v.as_str()).unwrap_or(""), - res.get("stderr").and_then(|v| v.as_str()).unwrap_or(""), - ), - ) - }) - .unwrap_or_else(|err| (StatusCode::BAD_REQUEST, format!("Error: {:?}", err))); - temp_dir.release(); - Response::builder() - .status(code) - .body(Body::from(msg)) - .unwrap_or_else(|err| Response::new(Body::from(format!("{}", err)))) - } - })) -} + // spawn job + thread::spawn(move || { + req.into_body().concat2().map(move |body| { + let body = String::from_utf8(body.to_vec()).unwrap(); + let req_obj = json!({ + "body": body, + "headers": headers, + "method": method, + }); + let hooks = HOOKS.lock().unwrap(); + { + let mut temp_dir = Temp::new_dir().unwrap(); + let temp_path = temp_dir.to_path_buf(); + assert!(temp_path.exists()); -/* pub fn dip_service( - req: Request, -) -> Box, Error = String> + Send> { - let uri = req.uri().path().to_owned(); - Box::new(future::ok(service_fn(req).unwrap_or_else(|err| { - eprintln!("Error from '{}': {}", uri, err); + let hook = hooks.get(&name).unwrap(); + let (code, msg) = hook.iter() + .fold(Ok(req_obj), |prev, handler| { + prev.and_then(|val| { + println!("Running {}...", handler.config()); + let result = handler.run(&temp_path, val); + println!("{:?}", result); + result + }) + }) + .map(|res| { + ( + StatusCode::ACCEPTED, + format!( + "stdout:\n{}\n\nstderr:\n{}", + res.get("stdout").and_then(|v| v.as_str()).unwrap_or(""), + res.get("stderr").and_then(|v| v.as_str()).unwrap_or(""), + ), + ) + }) + .unwrap_or_else(|err| (StatusCode::BAD_REQUEST, format!("Error: {:?}", err))); + + temp_dir.release(); + Response::builder() + .status(code) + .body(Body::from(msg)) + .unwrap_or_else(|err| Response::new(Body::from(format!("{}", err)))) + } + }) + }); + + Box::new(future::ok( Response::builder() - .status(StatusCode::NOT_FOUND) - .body(Body::from(NOTFOUND)) - .unwrap() - }))) + .status(StatusCode::ACCEPTED) + // TODO: assign job a uuid and do some logging + .body(Body::from(format!("job {} started", -1))) + .unwrap(), + )) } -*/