I am trying to develop a hyper based server application in Rust. There is an INI file holding configuration like binding IP, database and so on.
I don\'t want to parse
The two levels of closures in serve
are to be observed with care.
The closure at the second level (which is passed to service_fn_ok
), defined with move
, will attempt to move the only instance cfg
into it, even before any call to clone()
is made.
This move cannot be done multiple times without cloning, and therefore the closure will only implement FnOnce
. This is a case of double move: the second closure wants to receive a resource in an environment that only allows it to do that once.
To solve this problem, we want the first closure to receive cfg
, and clone it each time there.
fn main() {
let cfg = read_ini("demo.ini");
let addr = "127.0.0.1:3000".parse().unwrap();
let server = Server::bind(&addr)
.serve(move || {
let cfg = cfg.clone();
service_fn_ok(move |req| request_handler(req, &cfg))
})
.map_err(|e| println!("server error: {}", e));
rt::run(server);
}