2018-08-16 15:05:58 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2018-08-16 16:49:29 +00:00
|
|
|
use futures::{future, Future, Stream};
|
|
|
|
use hyper::{Body, Error, Request, Response, StatusCode};
|
2018-08-16 20:42:28 +00:00
|
|
|
use mktemp::Temp;
|
2018-08-16 15:05:58 +00:00
|
|
|
|
2018-08-16 16:49:29 +00:00
|
|
|
use {HOOKS, URIPATTERN};
|
2018-08-16 15:05:58 +00:00
|
|
|
|
2018-08-16 16:49:29 +00:00
|
|
|
pub fn dip_service(req: Request<Body>) -> Box<Future<Item = Response<Body>, Error = Error> + Send> {
|
2018-08-16 15:05:58 +00:00
|
|
|
let path = req.uri().path().to_owned();
|
2018-08-16 16:49:29 +00:00
|
|
|
let captures = match URIPATTERN.captures(path.as_ref()) {
|
|
|
|
Some(value) => value,
|
|
|
|
None => {
|
|
|
|
return Box::new(future::ok(
|
|
|
|
Response::builder()
|
|
|
|
.status(StatusCode::NOT_FOUND)
|
|
|
|
.body(Body::from("not found"))
|
|
|
|
.unwrap(),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let name = match captures.name("name") {
|
|
|
|
Some(value) => value.as_str().to_owned(),
|
|
|
|
None => {
|
|
|
|
return Box::new(future::ok(
|
|
|
|
Response::builder()
|
|
|
|
.status(StatusCode::NOT_FOUND)
|
|
|
|
.body(Body::from("not found"))
|
|
|
|
.unwrap(),
|
|
|
|
))
|
|
|
|
}
|
2018-08-16 15:05:58 +00:00
|
|
|
};
|
2018-08-16 16:49:29 +00:00
|
|
|
|
|
|
|
// TODO: filter by method as well
|
|
|
|
|
2018-08-21 01:53:37 +00:00
|
|
|
let headers = req
|
|
|
|
.headers()
|
2018-08-16 16:49:29 +00:00
|
|
|
.clone()
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|(k, v)| {
|
|
|
|
let key = k.unwrap().as_str().to_owned();
|
|
|
|
v.to_str().map(|value| (key, value.to_owned())).ok()
|
2018-08-21 01:53:37 +00:00
|
|
|
}).collect::<HashMap<_, _>>();
|
2018-08-16 16:49:29 +00:00
|
|
|
let method = req.method().as_str().to_owned();
|
2018-08-16 20:42:28 +00:00
|
|
|
|
2018-08-16 21:06:23 +00:00
|
|
|
// spawn job
|
2018-08-21 01:53:37 +00:00
|
|
|
Box::new(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());
|
2018-08-16 21:06:23 +00:00
|
|
|
|
2018-08-21 01:53:37 +00:00
|
|
|
let hook = hooks.get(&name).unwrap();
|
|
|
|
let (code, msg) = match hook.handle(req_obj, temp_path) {
|
|
|
|
Ok(msg) => (StatusCode::ACCEPTED, msg),
|
|
|
|
Err(msg) => (StatusCode::BAD_REQUEST, msg),
|
|
|
|
};
|
2018-08-16 21:06:23 +00:00
|
|
|
|
2018-08-21 01:53:37 +00:00
|
|
|
temp_dir.release();
|
|
|
|
Response::builder()
|
|
|
|
.status(code)
|
|
|
|
.body(Body::from(msg))
|
|
|
|
.unwrap_or_else(|err| Response::new(Body::from(format!("{}", err))))
|
|
|
|
}
|
|
|
|
}))
|
2018-08-16 15:05:58 +00:00
|
|
|
}
|