我想使用warp服务多个连接,以便我可以将每个 http 请求重定向到 https。这就是我现在要做的。
#[tokio::main]
async fn main() {
let http_routes = warp::path("http").map(|| {
println!("This is http server");
warp::redirect(Uri::from_static("https://127.0.0.1:2443"))
});
let https_routes = warp::any().map(|| {
println!("This is https server");
"Hello, World! You are connected through tls connection."
});
let _http_warp = warp::serve(http_routes)
.run(([127, 0, 0, 1], 2080)).await;
let _https_warp = warp::serve(https_routes)
.tls()
.cert_path("sec/dmp.cert")
.key_path("sec/dmp.key")
.run(([127, 0, 0, 1], 2443)).await;
}
这不能按预期工作。它只侦听端口 2080 而不是 2443
我也试过future::join
#[tokio::main]
async fn main() {
----snip----
let (_http_addr, http_warp) = warp::serve(http_routes)
.bind_ephemeral(([127, 0, 0, 1], 2080));
let (_https_addr, https_warp) = warp::serve(https_routes)
.tls()
.cert_path("sec/dmp.cert")
.key_path("sec/dmp.key")
.bind_ephemeral(([127, 0, 0, 1], 2443));
future::join(http_warp, https_warp).await?
}
这给出了错误
error[E0277]: the `?` operator can only be applied to values that implement `std::ops::Try`
--> src/main.rs:27:5
|
27 | future::join(http_wrap, https_wrap).await?
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the `?` operator cannot be applied to type `((), ())`
|
= help: the trait `std::ops::Try` is not implemented for `((), ())`
= note: required by `std::ops::Try::into_result`