This prolem is related to How to pass variables to hyper service handler? but I am not able to figure it out between my async functions.
I am running a tool which takes a path as a command line argument and starts a webserver which is launching a service that will scrape some file from the given path.
I am having trouble passing the path to the service handler as it seems to be running out of scope because its living in another thread.
This is my main function, I have added type information as comments
#[tokio::main]
async fn main() -> Result<()> {
let mut args: Vec<String> = env::args().collect();
args.remove(0);
// (PathBuf, log::Level)
let (path, log_level) = handle_args(args)?;
simple_logger::init_with_level(log_level)?;
let addr = ([127, 0, 0, 1], 9898).into();
info!("Listening on http://{}", addr);
let make_svc = make_service_fn(|_| async {
// async fn serve_req(path: &Path) -> std::result::Result<Response<Body>, hyper::Error>
Ok::<_, hyper::Error>(service_fn(|_req| async { serve_req(path.as_path()).await }))
});
// Then bind and serve...
let server = Server::bind(&addr).serve(make_svc);
if let Err(err) = server.await {
error!("server error: {}", err);
}
Ok(())
}
The compiler is giving me the following error:
error[E0597]: `path` does not live long enough
--> src/main.rs:43:67
|
42 | let make_svc = make_service_fn(|_| async {
| ____________________________________---_-
| | |
| | value captured here
43 | | Ok::<_, hyper::Error>(service_fn(|_req| async { serve_req(path.as_path()).await }))
| | ^^^^ borrowed value does not live long enough
44 | | });
| |_____- returning this value requires that `path` is borrowed for `'static`
...
54 | }
| - `path` dropped here while still borrowed
I think I understand the problem but I have not been able to clone/move/copy the path into the service handler in a way that it does not fall out of scope.
How would I approach this?