This repository was archived by the owner on Apr 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathconn_client.rs
More file actions
41 lines (39 loc) · 1.53 KB
/
conn_client.rs
File metadata and controls
41 lines (39 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use futures::{future, Future};
use http::{Request, Uri};
use hyper::client::connect::{Destination, HttpConnector};
use hyper::rt;
use tokio_buf::util::BufStreamExt;
use tower::MakeService;
use tower::{Service, ServiceExt};
use tower_http_util::body::BodyExt;
use tower_hyper::client::Connect;
use tower_hyper::util::Connector;
fn main() {
pretty_env_logger::init();
rt::run(future::lazy(|| {
let dst = Destination::try_from_uri(Uri::from_static("http://127.0.0.1:3000")).unwrap();
let connector = Connector::new(HttpConnector::new(1));
let mut hyper = Connect::new(connector);
hyper
.make_service(dst)
.map_err(|err| eprintln!("Connect Error {:?}", err))
.and_then(|conn| {
conn.ready()
.and_then(|mut conn| conn.call(Request::new(Vec::new())))
.map_err(|e| eprintln!("Call Error: {}", e))
.and_then(|response| {
println!("Response Status: {:?}", response.status());
response
.into_body()
.into_buf_stream()
.collect::<Vec<u8>>()
.map(|v| String::from_utf8(v).unwrap())
.map_err(|e| eprintln!("Body Error: {:?}", e))
})
.and_then(|body| {
println!("Response Body: {}", body);
Ok(())
})
})
}));
}