From d1d925e55e8a75e72813a647fa2fe843023f8980 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Mon, 17 Aug 2020 23:23:14 -0700 Subject: copy example skeleton from rust async book --- src/main.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/main.rs (limited to 'src/main.rs') 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) -> 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; +} -- cgit v1.2.3