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
42
43
44
45
46
47
|
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Client, Request, Response, Server, Uri};
use std::net::SocketAddr;
async fn upstream_req(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
println!("hit: {}", req.uri());
let req_uri = req.uri();
let upstream_uri = Uri::builder()
.scheme("http")
.authority("localhost:9200")
.path_and_query(req_uri.path_and_query().unwrap().as_str())
.build()
.unwrap();
let res = Client::new().get(upstream_uri).await?;
println!("resp!");
Ok(res)
}
async fn shutdown_signal() {
// Wait for the CTRL+C signal
tokio::signal::ctrl_c()
.await
.expect("failed to install CTRL+C signal handler");
}
async fn run_server(addr: SocketAddr) {
println!("Listening on http://{}", addr);
let serve_future = Server::bind(&addr)
.serve(make_service_fn(|_| async {
Ok::<_, hyper::Error>(service_fn(upstream_req))
}));
let graceful = serve_future.with_graceful_shutdown(shutdown_signal());
if let Err(e) = graceful.await {
eprintln!("server error: {}", e);
}
}
#[tokio::main]
async fn main() {
let addr = SocketAddr::from(([127, 0, 0, 1], 3030));
run_server(addr).await;
}
|