Dear Rustaceans,
What is the correct way to handle HTTP requests with parameters using hyper and async functions ?
Concretely, here is a code example:
- In the
serve_file
function, I receive afilepath
variable I got with clap argument parser - I clone and convert it to
String
- I give it to the
handle_request
function.
I have compiler errors with static variables and lifetimes.
/// Handle request
async fn handle_request(req: Request<Body>, filepath: String) -> Result<Response<Body>> {
match (req.method(), req.uri().path()) {
(&Method::GET, filepath) => send_file(filepath).await,
_ => Ok(not_found()),
}
}
/// Serve requests
#[tokio::main]
pub async fn serve_file(filepath: &str, ip_addr: &str, port: &str, count: &str) -> Result<()> {
// Create address
let addr = format!("{ip_addr}:{port}", ip_addr = ip_addr, port = port)
.parse()
.unwrap();
// Create service
let service = make_service_fn(move |_| async {
let filepath = filepath.clone();
let filepath = filepath.to_owned();
Ok::<_, hyper::Error>(service_fn(|req| handle_request(req, filepath)))
});
// Create server bound on the provided address
let server = Server::bind(&addr).serve(service);
// Wait for the server to complete serving or exit with an error.
if let Err(error) = server.await {
eprintln!("Error: {}", error);
}
Ok(())
}
let server = Server::bind(&addr).serve(service);
^^^^^ lifetime `'static` required
Thank you very much for sharing your knowledge.