From 4f010d9b2d2ffbe7394add5df5242b998de67786 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9mLol=20St=C3=BAdi=C3=B3?= <59310460+FemLolStudio@users.noreply.github.com> Date: Fri, 29 Dec 2023 01:07:58 +0100 Subject: [PATCH] Updating `tls-rustls` example (#2457) --- examples/tls-rustls/Cargo.toml | 2 +- examples/tls-rustls/src/main.rs | 74 ++++++++++++++++----------------- 2 files changed, 37 insertions(+), 39 deletions(-) diff --git a/examples/tls-rustls/Cargo.toml b/examples/tls-rustls/Cargo.toml index 0edf74d677..4c255c2763 100644 --- a/examples/tls-rustls/Cargo.toml +++ b/examples/tls-rustls/Cargo.toml @@ -6,7 +6,7 @@ publish = false [dependencies] axum = { path = "../../axum" } -axum-server = { version = "0.3", features = ["tls-rustls"] } +axum-server = { version = "0.6", features = ["tls-rustls"] } tokio = { version = "1", features = ["full"] } tracing = "0.1" tracing-subscriber = { version = "0.3", features = ["env-filter"] } diff --git a/examples/tls-rustls/src/main.rs b/examples/tls-rustls/src/main.rs index 441eefda16..3649c75cb1 100644 --- a/examples/tls-rustls/src/main.rs +++ b/examples/tls-rustls/src/main.rs @@ -27,44 +27,42 @@ struct Ports { #[tokio::main] async fn main() { - // Updating this example to hyper 1.0 requires axum_server to update first - - // tracing_subscriber::registry() - // .with( - // tracing_subscriber::EnvFilter::try_from_default_env() - // .unwrap_or_else(|_| "example_tls_rustls=debug".into()), - // ) - // .with(tracing_subscriber::fmt::layer()) - // .init(); - - // let ports = Ports { - // http: 7878, - // https: 3000, - // }; - // // optional: spawn a second server to redirect http requests to this server - // tokio::spawn(redirect_http_to_https(ports)); - - // // configure certificate and private key used by https - // let config = RustlsConfig::from_pem_file( - // PathBuf::from(env!("CARGO_MANIFEST_DIR")) - // .join("self_signed_certs") - // .join("cert.pem"), - // PathBuf::from(env!("CARGO_MANIFEST_DIR")) - // .join("self_signed_certs") - // .join("key.pem"), - // ) - // .await - // .unwrap(); - - // let app = Router::new().route("/", get(handler)); - - // // run https server - // let addr = SocketAddr::from(([127, 0, 0, 1], ports.https)); - // tracing::debug!("listening on {}", addr); - // axum_server::bind_rustls(addr, config) - - // .await - // .unwrap(); + tracing_subscriber::registry() + .with( + tracing_subscriber::EnvFilter::try_from_default_env() + .unwrap_or_else(|_| "example_tls_rustls=debug".into()), + ) + .with(tracing_subscriber::fmt::layer()) + .init(); + + let ports = Ports { + http: 7878, + https: 3000, + }; + // optional: spawn a second server to redirect http requests to this server + tokio::spawn(redirect_http_to_https(ports)); + + // configure certificate and private key used by https + let config = RustlsConfig::from_pem_file( + PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("self_signed_certs") + .join("cert.pem"), + PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("self_signed_certs") + .join("key.pem"), + ) + .await + .unwrap(); + + let app = Router::new().route("/", get(handler)); + + // run https server + let addr = SocketAddr::from(([127, 0, 0, 1], ports.https)); + tracing::debug!("listening on {}", addr); + axum_server::bind_rustls(addr, config) + .serve(app.into_make_service()) + .await + .unwrap(); } #[allow(dead_code)]