Files
axum/examples/tls-rustls/src/main.rs
T
David PedersenandJonas Platte fd70f81c46 More robust asset paths in examples (#1090)
* More robust asset paths in examples

* Update examples/low-level-rustls/src/main.rs

Co-authored-by: Jonas Platte <[email protected]>

* format

Co-authored-by: Jonas Platte <[email protected]>
2022-06-15 22:42:49 +02:00

45 lines
1.2 KiB
Rust

//! Run with
//!
//! ```not_rust
//! cd examples && cargo run -p example-tls-rustls
//! ```
use axum::{routing::get, Router};
use axum_server::tls_rustls::RustlsConfig;
use std::{net::SocketAddr, path::PathBuf};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
#[tokio::main]
async fn main() {
tracing_subscriber::registry()
.with(tracing_subscriber::EnvFilter::new(
std::env::var("RUST_LOG").unwrap_or_else(|_| "example_tls_rustls=debug".into()),
))
.with(tracing_subscriber::fmt::layer())
.init();
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));
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();
}
async fn handler() -> &'static str {
"Hello, World!"
}