sslrusttls1.2rust-axum

Axum + TLS | How to specify config?


Before axum I've worked with rust rocket framework. Rocket provides special file Rocket.toml where configuration can be specified. In there I wrote key and certs paths, but how to do the same in axum?

I have checked axum's examples and tried to use axum-server, but it looks like dead? I got an error with dependecies and don't know how to fix that. Even i don't know is there any sense to create an issue in axum-server repo.

If someone knows the different way - it would be great!
Error:

Copying platform assembly files from C:/Users/*/.cargo/registry/src/index.crates.io-6f17d22bba15001f/aws-lc-sys-0.24.0/aws-lc/generated-src/win-x86_64/crypto/ to D:/Rust/test-repo/target/debug/build/aws-lc-sys-5c959976a3609b0f/out/build/aws-lc/crypto
  mingw32-make[2]: *** No rule to make target 'aws-lc/crypto/CMakeFiles/crypto_objects.dir/chacha/chacha-x86_64.asm.obj', needed by 'artifacts/libaws_lc_0_24_0_crypto.a'.  Stop.
  mingw32-make[1]: *** [CMakeFiles\Makefile2:272: aws-lc/crypto/CMakeFiles/crypto.dir/all] Error 2
  mingw32-make: *** [Makefile:135: all] Error 2
  thread 'main' panicked at C:\Users\*\.cargo\registry\src\index.crates.io-6f17d22bba15001f\cmake-0.1.52\src/lib.rs:1115:5:

Solution

  • Here is a minimal example using either tls-rustls or tls-openssl with axum-server.

    /*
    [dependencies.axum]
    version = ">=0"
    default-features = false
    
    [dependencies.axum-server]
    version = ">=0"
    default-features = false
    features = ["tls-rustls", "tls-openssl"] # choose only one of these
    
    [dependencies.tokio]
    version = ">=0"
    default-features = false
    features = ["rt-multi-thread"]
    */
    
    async fn get_root() -> axum::response::Response {
        let content_type = "text/html; charset=UTF-8";
        let content = r#"<!DOCTYPE html>
        <html><head>
        <meta charset="utf-8">
        </head><body>
        <hr><h2>Hello</h2><hr>
        </body></html>
        "#;
        axum::response::Response::builder()
            .status(axum::http::StatusCode::OK)
            .header(axum::http::header::CONTENT_TYPE, content_type)
            .body(content.into())
            .unwrap()
    }
    
    #[tokio::main]
    async fn main() {
        let port_number = 9988_u16;
        let bind_addr = std::net::SocketAddr::from((
            std::net::Ipv4Addr::UNSPECIFIED,
            port_number,
        ));
        //
        let app = axum::Router::new().route("/", axum::routing::get(get_root));
        //
        let cert_file = std::path::Path::new("cert.pem");
        let key_file = std::path::Path::new("key.pem");
        //
        if std::env::args().any(|a| a == "ssl") {
            println!("using openssl on port {}", port_number);
            let ssl_config =
                axum_server::tls_openssl::OpenSSLConfig::from_pem_file(
                    cert_file, key_file,
                )
                .unwrap();
            let server = axum_server::bind_openssl(bind_addr, ssl_config);
            server.serve(app.into_make_service()).await.unwrap();
        } else {
            println!("using rustls on port {}", port_number);
            let tls_config =
                axum_server::tls_rustls::RustlsConfig::from_pem_file(
                    cert_file, key_file,
                )
                .await
                .unwrap();
            let server = axum_server::bind_rustls(bind_addr, tls_config);
            server.serve(app.into_make_service()).await.unwrap();
        }
    }