Closing multiple hyper servers

Hi there. I'm creating a server, for this I have the following template code:

use hyper::{Body, Request, Response, Server};
use hyper::service::service_fn_ok;
use hyper::rt::{self, Future};

fn main() {
    let addr = ([127, 0, 0, 1], 3000).into();

    let server = Server::bind(&addr)
        .serve(|| {
            service_fn_ok(move |_: Request<Body>| {
                Response::new(Body::from("Hello World!"))
                //CLOSE SERVER HERE
            })
        })
        .map_err(|e| eprintln!("server error: {}", e));

    println!("Listening on http://{}", addr);

    rt::run(server);
}

However, I'm trying to close the server on a http request. How can I achieve this?

with_graceful_shutdown, modify example so to move tx into the closure.

Thanks, thats what I needed. However I'm not able to move the tx into the closure, because of expected a closure that implements the Fntrait, but this closure only implementsFnOnce

This is what I have so far:

use hyper::rt::{self, Future};
use hyper::service::service_fn_ok;
use hyper::{Body, Request, Response, Server};
use std::sync::{Arc, Mutex};

fn main() {
    let addr = ([127, 0, 0, 1], 3000).into();
    let (tx, rx) = futures::sync::oneshot::channel::<()>();
    let safe = Arc::new(Mutex::new(tx));
    let server = Server::bind(&addr).serve(|| {
        service_fn_ok(move |_: Request<Body>| {
            let safe = safe.clone();
            safe.lock().unwrap().send(());
            Response::new(Body::from("Hello World!"))
        })
    });
    let graceful = server
        .with_graceful_shutdown(rx)
        .map_err(|err| eprintln!("server error: {}", err));

    println!("Listening on http://{}", addr);
    rt::run(graceful);
}