1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
use axum::{routing::get, Router};
use futures_util::future::try_join_all;
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr};

#[tokio::main]
async fn main() {
    let servers = vec![
        SocketAddr::new(Ipv4Addr::LOCALHOST.into(), 3000),
        SocketAddr::new(Ipv6Addr::LOCALHOST.into(), 3000),
    ]
    .into_iter()
    .map(|addr| tokio::spawn(start_server(addr)));

    // Returns the first error if any of the servers return an error.
    try_join_all(servers).await.unwrap();
}

async fn start_server(addr: SocketAddr) {
    let app = Router::new().route("/", get(|| async { "Hello, world!" }));

    axum_server::bind(addr)
        .serve(app.into_make_service())
        .await
        .unwrap();
}