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

Function main

https-tls/acme-letsencrypt/src/main.rs:139–181  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

137
138#[actix_web::main]
139async fn main() -> eyre::Result<()> {
140 env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
141 color_eyre::install()?;
142
143 rustls::crypto::aws_lc_rs::default_provider()
144 .install_default()
145 .unwrap();
146
147 // Load keys
148 // ==============================================
149 // = IMPORTANT: =
150 // = This process has to be repeated =
151 // = before the certificate expires (< 90 days) =
152 // ==============================================
153 // Obtain TLS certificate
154 //
155 // NOTE: Persisting the private key and certificate chain somewhere is
156 // recommended in order to avoid unnecessarily regenerating of TLS certs.
157 let cert = gen_tls_cert(DOMAIN_NAME, CONTACT_EMAIL).await?;
158
159 let rustls_config = load_rustls_config(cert)?;
160
161 log::info!("starting HTTP server at https://{DOMAIN_NAME}:443");
162
163 // Start HTTP server!
164 let srv = HttpServer::new(|| App::new().route("/", web::get().to(index)))
165 .bind_rustls_0_23(("0.0.0.0", 443), rustls_config)?
166 .run();
167
168 let srv_handle = srv.handle();
169
170 let _auto_shutdown_task = rt::spawn(async move {
171 // Shutdown server every 4 weeks so that TLS certs can be regenerated if
172 // needed. This is only appropriate in contexts like Kubernetes which
173 // can orchestrate restarts.
174 rt::time::sleep(Duration::from_secs(60 * 60 * 24 * 28)).await;
175 srv_handle.stop(true).await;
176 });
177
178 srv.await?;
179
180 Ok(())
181}
182
183fn load_rustls_config(cert: Certificate) -> eyre::Result<rustls::ServerConfig> {
184 // init server config builder with safe defaults

Callers

nothing calls this directly

Calls 6

gen_tls_certFunction · 0.85
sleepFunction · 0.85
runMethod · 0.80
stopMethod · 0.80
load_rustls_configFunction · 0.70
handleMethod · 0.45

Tested by

no test coverage detected