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
) -> Result, 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;
}