()
| 23 | |
| 24 | #[tokio::main(flavor = "current_thread")] |
| 25 | async fn main() -> eyre::Result<()> { |
| 26 | color_eyre::install()?; |
| 27 | examples_common::init_standard_logger(); |
| 28 | examples_common::init_rustls_provider(); |
| 29 | |
| 30 | // initial load of certificate and key files |
| 31 | let cert_key = load_certified_key()?; |
| 32 | |
| 33 | // signal channel used to notify rustls of cert/key file changes |
| 34 | let (reload_tx, cert_resolver) = rustls_channel_resolver::channel::<8>(cert_key); |
| 35 | |
| 36 | let rustls_config = ServerConfig::builder() |
| 37 | .with_no_client_auth() |
| 38 | .with_cert_resolver(cert_resolver); |
| 39 | |
| 40 | // unsupervised watcher thread which will just shutdown when the server stops |
| 41 | tracing::debug!("Setting up cert watcher"); |
| 42 | |
| 43 | let mut file_watcher = |
| 44 | notify::recommended_watcher(move |res: notify::Result<Event>| match res { |
| 45 | Ok(ev) => { |
| 46 | tracing::info!("files changed: {:?}", ev.paths); |
| 47 | |
| 48 | let cert_key = load_certified_key().unwrap(); |
| 49 | reload_tx.update(cert_key); |
| 50 | } |
| 51 | Err(err) => { |
| 52 | tracing::error!("file watch error: {err}"); |
| 53 | } |
| 54 | }) |
| 55 | .wrap_err("Failed to set up file watcher")?; |
| 56 | |
| 57 | file_watcher |
| 58 | .watch(Path::new("cert.pem"), RecursiveMode::NonRecursive) |
| 59 | .wrap_err("Failed to watch cert file")?; |
| 60 | file_watcher |
| 61 | .watch(Path::new("key.pem"), RecursiveMode::NonRecursive) |
| 62 | .wrap_err("Failed to watch key file")?; |
| 63 | |
| 64 | tracing::info!("Starting HTTPS server at https://localhost:8443"); |
| 65 | |
| 66 | // start running server as normal (as opposed to in a loop like the cert-watch example) |
| 67 | HttpServer::new(|| { |
| 68 | App::new() |
| 69 | .service(web::resource("/").to(index)) |
| 70 | .wrap(middleware::Logger::default().log_target("@")) |
| 71 | }) |
| 72 | .workers(2) |
| 73 | .bind_rustls_0_23(("127.0.0.1", 8443), rustls_config)? |
| 74 | .run() |
| 75 | .await?; |
| 76 | |
| 77 | Ok(()) |
| 78 | } |
| 79 | |
| 80 | fn load_certified_key() -> eyre::Result<rustls::sign::CertifiedKey> { |
| 81 | // load TLS key/cert files |
nothing calls this directly
no test coverage detected