WIP: Futures not working

This commit is contained in:
Brandon Thomas
2020-06-29 06:55:24 -04:00
parent aa7345be2d
commit 09ed0163af
+59 -10
View File
@@ -125,6 +125,7 @@ fn main() -> AnyhowResult<()> {
let remote_addr = socket.remote_addr(); let remote_addr = socket.remote_addr();
info!("Got a request from: {:?}", remote_addr); info!("Got a request from: {:?}", remote_addr);
let route_a = route_one.clone(); let route_a = route_one.clone();
let route_b = route_two.clone(); let route_b = route_two.clone();
@@ -132,12 +133,15 @@ fn main() -> AnyhowResult<()> {
match req.method() { match req.method() {
&Method::OPTIONS => { &Method::OPTIONS => {
// TODO: CORS should be hardcoded here, not wastefully proxied. // TODO: CORS should be hardcoded here, not wastefully proxied.
return hyper_reverse_proxy::call(remote_addr.ip(), &route_a, req); return hyper_reverse_proxy::call(remote_addr.ip(), &route_a, req); // Return BoxFut
}, },
_ => {}, _ => {},
} }
let remote_addr2 = remote_addr.clone();
if req.uri().path().eq("/speak") { if req.uri().path().eq("/speak") {
info!("/speak"); info!("/speak");
let (_parts, body) = req.into_parts(); let (_parts, body) = req.into_parts();
@@ -152,24 +156,69 @@ fn main() -> AnyhowResult<()> {
} else if req.uri().path().eq("/speak_spectrogram") { } else if req.uri().path().eq("/speak_spectrogram") {
info!("/speak_spectrogram"); info!("/speak_spectrogram");
//let request : SpeakRequest = serde_json::from_str(&req.body().into()).unwrap();
//info!("Request: {:?}", request);
let (_parts, body) = req.into_parts(); let boxed = Box::new(req.into_body().concat2().map(move |b| { // Builds a BoxedFut to return
// Parse the request body. form_urlencoded::parse
// always succeeds, but in general parsing may
// fail (for example, an invalid post of json), so
// returning early with BadRequest may be
// necessary.
//
// Warning: this is a simplified use case. In
// principle names can appear multiple times in a
// form, and the values should be rolled up into a
// HashMap<String, Vec<String>>. However in this
// example the simpler approach is sufficient.
//let params = form_urlencoded::parse(b.as_ref()).into_owned().collect::<HashMap<String, String>>();
let body_bytes = body.concat2().wait().unwrap().into_bytes(); // Validate the request parameters, returning
// early if an invalid input is detected.
/*let name = if let Some(n) = params.get("name") {
n
} else {
return Response::builder()
.status(StatusCode::UNPROCESSABLE_ENTITY)
.body(MISSING.into())
.unwrap();
};
let number = if let Some(n) = params.get("number") {
if let Ok(v) = n.parse::<f64>() {
v
} else {
return Response::builder()
.status(StatusCode::UNPROCESSABLE_ENTITY)
.body(NOTNUMERIC.into())
.unwrap();
}
} else {
return Response::builder()
.status(StatusCode::UNPROCESSABLE_ENTITY)
.body(MISSING.into())
.unwrap();
};*/
let json_string = String::from_utf8(body_bytes.to_vec()); let name = "asdf";
let number = "asdf";
info!("Request String: {:?}", json_string);
let request: SpeakRequest = serde_json::from_slice(&body_bytes).unwrap(); // Render the response. This will often involve
// calls to a database or web service, which will
// require creating a new stream for the response
// body. Since those may fail, other error
// responses such as InternalServiceError may be
// needed here, too.
let body = format!("Hello {}, your number is {}", name, number);
Response::new(body.into())
print!("Request: {:?}", request); }));
Box::new(boxed.then( |b : Result<Response<Body>, Error> | {
let new_req = Request::new(Body::empty()); let new_req = Request::new(Body::empty());
hyper_reverse_proxy::call(remote_addr2.ip(), "http://127.0.0.1:12345/speak_request", new_req)
}))
return hyper_reverse_proxy::call(remote_addr.ip(), &route_b, new_req) //let new_req = Request::new(Body::empty());
//return hyper_reverse_proxy::call(remote_addr.ip(), &route_b, new_req)
} else { } else {
debug_request(req) debug_request(req)