Служить на нескольких портах (http, https) одновременно, используя платформу WARP в Rust - PullRequest
0 голосов
/ 25 апреля 2020

Я хочу обслуживать несколько соединений, используя 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

Я также пытался в будущем :: 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`

1 Ответ

0 голосов
/ 25 апреля 2020

Совершенно просто сделать этот запуск и удалить? суффикс из оператора await в конце и точка с запятой.

future::join(http_wrap, https_wrap).await? // FROM THIS
future::join(http_wrap, https_wrap).await; // TO THIS

И все. Теперь оба фьючерса работают одновременно, и поэтому он прослушивает оба порта.

...