I am developing a reverse proxy named palantir. It works fine and outperforms nginx reverse proxy in benchmarks. I am actually trying to migrate from actix-web v0.7.18 to built-in rust async. I have some problems which I think are because of futures-preview = { version = "=0.3.0-alpha.17", features = ["compat"] }. I am quite new to rust and the recent versions of hyper and future. I will be grateful if you could help me.
It is the the revised version of my code:
in Cargo.toml:
futures-preview = { version = "=0.3.0-alpha.17", features = ["compat"] }
hyper = "0.12.9"
proxy.rs:
use futures::future::{self, Future};
use {hyper::{Body, Request, Response, Client, Uri, StatusCode}};
use std::str::FromStr;
type BoxFut = Box<dyn Future<Output=Response<Body>> + Send>;
fn forward_uri<B>(forward_url: &str, req: &Request<B>) -> Uri {
let forward_uri = match req.uri().query() {
Some(query) => format!("{}{}?{}", forward_url, req.uri().path(), query),
None => format!("{}{}", forward_url, req.uri().path()),
};
Uri::from_str(forward_uri.as_str()).unwrap()
}
pub fn call(forward_url: &str, mut request: Request<Body>) -> BoxFut {
*request.uri_mut() = forward_uri(forward_url, &request);
let proxied_request = request;
let client = Client::new();
use futures::future::Future;
let response = client.request(proxied_request).then(|response| {
let proxied_response = match response {
Ok(response) => response,
Err(_) => {
// println!("Error: {}", error); // TODO: Configurable logging
Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(Body::empty())
.unwrap()
},
};
future::ok(proxied_response)
});
Box::new(response)
}
First, I receive this error for then in the call function:
no method named then found for type hyper::client::ResponseFuture in the current scope
Second, I cannot use Error=hyper::Error in BoxFut type, because of this error:
associated type Error not found for futures::Future
This code works fine in the crate hyper-reverse-proxy which uses hyper = "0.12" and futures = "0.1".