How can I stop the hyper HTTP web server and return an error?

£可爱£侵袭症+ 提交于 2019-12-06 22:02:28

I believe I found a solution using the tokio runtime directly... It compiles. Please anyone tell me if I'm wrong here.

The idea is to use the tokio Runtime directly (docs have many examples):

use tokio::runtime::Runtime;

// Executes a web server. Returns a result object with an error object
// if the server stopped unexpectedly
fn run_and_stop_web_server(addr: std::net::SocketAddr) -> Result<(), Error> {
    let server_builder = Server::try_bind(&addr)?;
    let server = server_builder.serve(move || {
        let handler: Handler = Handler::new();
        hyper::service::service_fn(move |req| handler.serve(req))
    });

    let mut rt = Runtime::new()?;
    rt.block_on(server)?;
    rt.shutdown_now();

    Result::Ok(())
}

Maybe you can store the error into a local variable and return that:

let mut result = Result::Ok(());
hyper::rt::run(server.map_err(|e| { result = Result::Error (e); });
result
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!