Files
axum/examples/tls-rustls/src/main.rs
T

41 lines
1.1 KiB
Rust
Raw Normal View History

2021-08-02 23:09:09 +02:00
//! Run with
//!
//! ```not_rust
2022-04-29 18:53:41 +02:00
//! cd examples && cargo run -p example-tls-rustls
2021-08-02 23:09:09 +02:00
//! ```
2021-11-10 17:07:09 +03:00
use axum::{routing::get, Router};
use axum_server::tls_rustls::RustlsConfig;
use std::net::SocketAddr;
2022-03-06 12:37:00 +01:00
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
2021-08-01 09:32:47 +03:00
2021-11-10 17:07:09 +03:00
#[tokio::main]
async fn main() {
2022-03-06 12:37:00 +01:00
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();
2021-11-10 17:07:09 +03:00
let config = RustlsConfig::from_pem_file(
"examples/tls-rustls/self_signed_certs/cert.pem",
"examples/tls-rustls/self_signed_certs/key.pem",
)
.await
.unwrap();
2021-08-01 09:32:47 +03:00
2021-11-10 17:07:09 +03:00
let app = Router::new().route("/", get(handler));
2021-08-01 09:32:47 +03:00
2021-11-10 17:07:09 +03:00
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();
}
2021-11-10 17:07:09 +03:00
async fn handler() -> &'static str {
"Hello, World!"
}