问题描述
这是一个简单的 actix-web 应用,配置为与 rustls 配合使用。此应用程序照常处理 get 请求,但不处理 post 请求。我正在使用 mkcert 生成证书。
Cargo.toml
actix-web = { version = "3.3.2",features = ["rustls"] }
rustls = { version = "0.18.0" }
main.rs
**//post request handler**
async fn post_req() -> impl Responder {
String::from("post req")
}
**//get request handler**
async fn get_req() -> impl Responder {
String::from("get req")
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
println!("Open app from https://127.0.0.1:8080");
let tls_config = tls_config(
"S:/Workspaces/Temp/rust/oslc/localhost+1.pem","S:/Workspaces/Temp/rust/oslc/localhost+1-key.pem",)
.unwrap();
HttpServer::new(move || {
App::new()
.service(web::resource("/post").route(web::post().to(post_req)))
.service(web::resource("/get").route(web::get().to(get_req)))
})
.bind_rustls("localhost:8080",tls_config.clone())?
.run()
.await
}
pub fn tls_config(
cert_file_path: &str,key_file_path: &str,) -> std::io::Result<rustls::ServerConfig> {
let mut config: rustls::ServerConfig = ServerConfig::new(NoClientAuth::new());
let cert_file = &mut BufReader::new(File::open(cert_file_path)?);
let key_file = &mut BufReader::new(File::open(key_file_path)?);
let cert_chain = certs(cert_file).unwrap();
let mut keys = pkcs8_private_keys(key_file).unwrap();
config.set_single_cert(cert_chain,keys.remove(0)).unwrap();
Ok(config)
}
邮递员回复
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)