diff options
author | Bryan Newbold <bnewbold@archive.org> | 2020-08-17 23:23:14 -0700 |
---|---|---|
committer | Bryan Newbold <bnewbold@archive.org> | 2020-08-17 23:23:14 -0700 |
commit | d1d925e55e8a75e72813a647fa2fe843023f8980 (patch) | |
tree | 2c35fec8c8557d3235b1c4646b8f9efe94b88930 /src | |
parent | f0aa8010401e3872f8f1dcc85c409e77c6b5a1d8 (diff) | |
download | es-public-proxy-d1d925e55e8a75e72813a647fa2fe843023f8980.tar.gz es-public-proxy-d1d925e55e8a75e72813a647fa2fe843023f8980.zip |
copy example skeleton from rust async book
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..92d030a --- /dev/null +++ b/src/main.rs @@ -0,0 +1,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; +} |