tokio 非异步函数创建HTTP服务

fn http_server() {
    std::thread::spawn(|| {
        let rt = tokio::runtime::Runtime::new().unwrap();
        rt.block_on(actix());
    });
}

#[actix_web::get("/*")]
async fn hello() -> String {
    format!("OKK")
}

async fn actix()  {
    let run = actix_web::HttpServer::new(|| {
        actix_web::App::new().service(hello)
    }).bind("0.0.0.0:8080").unwrap().run().await;
    if let Err(e) = run {
        println!("HTTP服务启动失败:{}", e);
    }
}
posted @ 2021-12-10 14:31  develon  阅读(119)  评论(0编辑  收藏  举报