kan*_*udo 7 future rust rust-warp
我想使用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;
}
Run Code Online (Sandbox Code Playgroud)
这不能按预期工作。它只侦听端口 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?
}
Run Code Online (Sandbox Code Playgroud)
这给出了错误
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`
Run Code Online (Sandbox Code Playgroud)
运行这个完全是一件简单的事情,那就是删除?来自await 语句的后缀在末尾并放置一个分号。
future::join(http_wrap, https_wrap).await? // FROM THIS
future::join(http_wrap, https_wrap).await; // TO THIS
Run Code Online (Sandbox Code Playgroud)
就是这样。现在,两个 future 同时运行,因此它会监听两个端口。
| 归档时间: |
|
| 查看次数: |
2561 次 |
| 最近记录: |