MCPcopy Create free account
hub / github.com/actix/examples / main

Function main

https-tls/cert-watch/src/main.rs:28–107  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

26
27#[tokio::main(flavor = "current_thread")]
28async fn main() -> eyre::Result<()> {
29 color_eyre::install()?;
30 env_logger::init_from_env(env_logger::Env::default().default_filter_or("info"));
31
32 // Load default provider, to be done once for the process
33 rustls::crypto::aws_lc_rs::default_provider()
34 .install_default()
35 .unwrap();
36
37 // signal channel used to notify main event loop of cert/key file changes
38 let (reload_tx, mut reload_rx) = mpsc::channel(1);
39
40 let mut file_watcher =
41 notify::recommended_watcher(move |res: notify::Result<Event>| match res {
42 Ok(ev) => {
43 log::info!("files changed: {:?}", ev.paths);
44 reload_tx.blocking_send(TlsUpdated).unwrap();
45 }
46 Err(err) => {
47 log::error!("file watch error: {err}");
48 }
49 })
50 .unwrap();
51
52 file_watcher
53 .watch(Path::new("cert.pem"), RecursiveMode::NonRecursive)
54 .unwrap();
55 file_watcher
56 .watch(Path::new("key.pem"), RecursiveMode::NonRecursive)
57 .unwrap();
58
59 // start HTTP server reload loop
60 //
61 // loop reloads on TLS changes and exits on normal ctrl-c (etc.) signals
62 loop {
63 // load TLS cert/key files
64 let config = load_rustls_config()?;
65
66 log::info!("starting HTTPS server at https://localhost:8443");
67
68 // start running server but don't await it
69 let mut server = HttpServer::new(|| {
70 App::new()
71 .service(web::resource("/").to(index))
72 .wrap(middleware::Logger::default())
73 })
74 .workers(2)
75 .bind_rustls_0_23("127.0.0.1:8443", config)?
76 .run();
77
78 // server handle to send signals
79 let server_hnd = server.handle();
80
81 tokio::select! {
82 // poll server continuously
83 res = &mut server => {
84 log::info!("server shut down via signal or manual command");
85 res?;

Callers

nothing calls this directly

Calls 3

runMethod · 0.80
load_rustls_configFunction · 0.70
handleMethod · 0.45

Tested by

no test coverage detected