How to shut down the server?
There's currently no way to do this. I'd like to know your use case to design a useful API that allows for (graceful?) shutdown.
I want to create a dynamic library loading/unloading tool, which starts a network service when the dynamic library is loaded, and shuts down the network service and releases the port before the dynamic library is unloaded.
Similar to the following code, or is there a better approach?
// static mut SERVER_HANDLE: Option<Arc<ServerHandle>> = None;
#[no_mangle]
pub extern "system" fn plugin_init() {
thread::spawn(||{
let server = Server::bind("localhost:3000");
// SERVER_HANDLE = Some(server);
server.serve(|_req, info|{
ResponseBuilder::new()
.header("Content-Type", "text/html")
.body(Body::new("hello world"))
.unwrap()
}).unwrap();
});
println!("Plugin initialized");
}
#[no_mangle]
pub extern "system" fn plugin_quit() {
// Do some cleanup here
// if let Some(handle) = &SERVER_HANDLE {
// handle.quit();
// }
println!("Plugin quited");
}
We could probably expose an incoming requests iterator to allow users to control the server loop, and use signal handling to exit as necessary. We would also need an API to wait/timeout on active requests in the thread pool.