如何解决发布请求不适用于 actix-web
这是一个简单的 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 举报,一经查实,本站将立刻删除。