我想使用 WRAP 这样我就可以将每个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端口。它只监听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`
要让它运行起来,完全是一件简单的事情,就是把 await 语句中的后缀去掉,然后加上一个分号。
future::join(http_wrap, https_wrap).await? // FROM THIS
future::join(http_wrap, https_wrap).await; // TO THIS
就可以了。现在,两个期货同时运行,因此它在两个端口上都能监听。