From a9dfdde9d5667f633586f492a997b3545ade4ce9 Mon Sep 17 00:00:00 2001 From: Daniel M Date: Thu, 24 Mar 2022 20:07:41 +0100 Subject: [PATCH] Tiny refactor --- src/haproxy.rs | 3 ++- src/main.rs | 20 ++++++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/haproxy.rs b/src/haproxy.rs index 61e9524..2a3413e 100644 --- a/src/haproxy.rs +++ b/src/haproxy.rs @@ -10,6 +10,7 @@ pub enum HaProxyErr { UpdateFailed, } +#[derive(Debug, Clone)] pub struct HaProxyApi { ip: String, port: u16, @@ -23,7 +24,7 @@ impl HaProxyApi { Self { ip, port, cert_dir } } - pub async fn update_cert>(&mut self, cert_path: T) -> Result<(), HaProxyErr> { + pub async fn update_cert>(&self, cert_path: T) -> Result<(), HaProxyErr> { let cert_path = cert_path.as_ref(); let cert_name = cert_path.file_name().unwrap().to_string_lossy().to_string(); let cert_content = tokio::fs::read_to_string(cert_path).await.unwrap(); diff --git a/src/main.rs b/src/main.rs index 8ad05bf..37daff6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use log::{debug, error, info}; use crate::{ certs::{load_cert_from_fullchain, AcmeApiEndpoint, CertExt, CertRequester}, config::Config, - http::ChallengeServer, haproxy::HaProxyApi, + http::{ChallengeServer, ChallengeManager}, haproxy::HaProxyApi, }; mod certs; @@ -32,7 +32,7 @@ async fn main() -> Result<(), Box> { debug!("Config file: {:#?}", &conf); - let mut haproxyapi = conf.haproxy.map(|ha| HaProxyApi::new(&ha.ip, ha.port, &ha.cert_dir)); + let haproxyapi = conf.clone().haproxy.map(|ha| HaProxyApi::new(&ha.ip, ha.port, &ha.cert_dir)); // Create the http server for serving the challenges let srv = ChallengeServer::new(&conf.http.ip, conf.http.port)?; @@ -44,8 +44,16 @@ async fn main() -> Result<(), Box> { srv.start().await.unwrap().0.await; }); + check_update_certs(&conf, &mgr, &haproxyapi).await; + + info!("All done. Shutting down"); + + Ok(()) +} + +pub async fn check_update_certs(conf: &Config, mgr: &ChallengeManager, haproxyapi: &Option) { // See what certs are requested in the config file - for (name, conf) in conf.certs { + for (name, conf) in &conf.certs { // Check if the cert needs to be created / renewed let should_renew = load_cert_from_fullchain(&conf.fullchain_file) .await @@ -70,7 +78,7 @@ async fn main() -> Result<(), Box> { info!("Certificate {name} needs to be renewed. Using endpoint: {endpoint}"); let requester = CertRequester::new(endpoint, conf.clone(), mgr.clone()); - match (requester.request_certs().await, &mut haproxyapi) { + match (requester.request_certs().await, haproxyapi) { (Ok(_), Some(api)) => { match api.update_cert(&conf.fullchain_file).await { Ok(()) => info!("Certificate update in haproxy completed"), @@ -84,8 +92,4 @@ async fn main() -> Result<(), Box> { info!("Certificate {name} does not need to be renewed"); } } - - info!("All done. Shutting down"); - - Ok(()) }