Created
May 6, 2020 20:39
-
-
Save avidal/0faae3906069440e3f9d5c307bede4fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![deny(warnings)] | |
use hyper::service::{make_service_fn, service_fn}; | |
use hyper::{Client, Server, Body, Request, Result, Response, StatusCode}; | |
async fn handle(req: Request<Body>) -> Result<Response<Body>> { | |
match req.uri().path() { | |
"/ping" => { | |
Ok(handle_ping()) | |
} | |
"/proxy" => { | |
// how do i avoid having to .await this? | |
handle_proxy(req).await | |
} | |
_ => Ok(handle_404()), | |
} | |
} | |
fn handle_404() -> Response<Body> { | |
Response::builder() | |
.status(StatusCode::NOT_FOUND) | |
.body("Not Found".into()) | |
.unwrap() | |
} | |
fn handle_ping() -> Response<Body> { | |
Response::builder() | |
.status(StatusCode::OK) | |
.body("pong".into()) | |
.unwrap() | |
} | |
// how do i align the function signatures between the different "handlers"? | |
// this one is async but returns a Response instead of a Future, and I had to .await it on the last line | |
// but I also had to await in the match arms above? | |
async fn handle_proxy(mut req: Request<Body>) -> Result<Response<Body>> { | |
let client = Client::new(); | |
let uri_string = format!( | |
"http://127.0.0.1:3001/{}", | |
req.uri().path_and_query().map(|x| x.as_str()).unwrap_or("") | |
); | |
let uri = uri_string.parse().unwrap(); | |
*req.uri_mut() = uri; | |
client.request(req).await | |
} | |
#[tokio::main] | |
async fn main() { | |
let bind_addr = ([127, 0, 0, 1], 3000).into(); | |
let make_service = make_service_fn(|_| async { Ok::<_, hyper::Error>(service_fn(handle)) }); | |
let server = Server::bind(&bind_addr).serve(make_service); | |
println!("Listening on http://{}", bind_addr); | |
if let Err(e) = server.await { | |
eprintln!("server error: {}", e); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment