Serve on multiple ports(http,https) simultaneously using WARP framework

I want to serve multiple connections using WARP so that I can redirect every http request to https. Here is what I am upto right now.

#[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;
}

This does not work as expected. It only listen on port 2080 and not on 2443

I also tried 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?
}

This give error

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`

Its completely a simple thing to make this run and that is removing ? suffix from await statement at the end and placing a semicolon.

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

And that's it. Now both the futures run simultaneously and thus it listens on both the ports.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.