From 2507463706d0cea90007b5959c579a32d4b24cc4 Mon Sep 17 00:00:00 2001 From: Eray Karatay <70590982+programatik29@users.noreply.github.com> Date: Wed, 10 Nov 2021 17:07:09 +0300 Subject: [PATCH] update tls-rustls example (#494) --- examples/tls-rustls/Cargo.toml | 2 +- examples/tls-rustls/src/main.rs | 50 ++++++++++++++++++--------------- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/examples/tls-rustls/Cargo.toml b/examples/tls-rustls/Cargo.toml index 764be233..428d41f2 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.2", features = ["tls-rustls"] } +axum-server = { version = "0.3", 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 0f7511be..666982b3 100644 --- a/examples/tls-rustls/src/main.rs +++ b/examples/tls-rustls/src/main.rs @@ -4,31 +4,35 @@ //! cargo run -p example-tls-rustls //! ``` -// NOTE: This example is currently broken since axum-server requires `S: Sync`, -// that isn't necessary and will be fixed in a future release +use axum::{routing::get, Router}; +use axum_server::tls_rustls::RustlsConfig; +use std::net::SocketAddr; -fn main() {} +#[tokio::main] +async fn main() { + // Set the RUST_LOG, if it hasn't been explicitly defined + if std::env::var_os("RUST_LOG").is_none() { + std::env::set_var("RUST_LOG", "example_tls_rustls=debug") + } + tracing_subscriber::fmt::init(); -// use axum::{handler::get, Router}; + let config = RustlsConfig::from_pem_file( + "examples/tls-rustls/self_signed_certs/cert.pem", + "examples/tls-rustls/self_signed_certs/key.pem", + ) + .await + .unwrap(); -// #[tokio::main] -// async fn main() { -// // Set the RUST_LOG, if it hasn't been explicitly defined -// if std::env::var_os("RUST_LOG").is_none() { -// std::env::set_var("RUST_LOG", "example_tls_rustls=debug") -// } -// tracing_subscriber::fmt::init(); + let app = Router::new().route("/", get(handler)); -// // let app = Router::new().route("/", get(handler)); + let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); + println!("listening on {}", addr); + axum_server::bind_rustls(addr, config) + .serve(app.into_make_service()) + .await + .unwrap(); +} -// // axum_server::bind_rustls("127.0.0.1:3000") -// // .private_key_file("examples/tls-rustls/self_signed_certs/key.pem") -// // .certificate_file("examples/tls-rustls/self_signed_certs/cert.pem") -// // .serve(app) -// // .await -// // .unwrap(); -// } - -// async fn handler() -> &'static str { -// "Hello, World!" -// } +async fn handler() -> &'static str { + "Hello, World!" +}