Tiny refactor
This commit is contained in:
parent
096ac29f26
commit
a9dfdde9d5
@ -10,6 +10,7 @@ pub enum HaProxyErr {
|
|||||||
UpdateFailed,
|
UpdateFailed,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
pub struct HaProxyApi {
|
pub struct HaProxyApi {
|
||||||
ip: String,
|
ip: String,
|
||||||
port: u16,
|
port: u16,
|
||||||
@ -23,7 +24,7 @@ impl HaProxyApi {
|
|||||||
Self { ip, port, cert_dir }
|
Self { ip, port, cert_dir }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn update_cert<T: AsRef<Path>>(&mut self, cert_path: T) -> Result<(), HaProxyErr> {
|
pub async fn update_cert<T: AsRef<Path>>(&self, cert_path: T) -> Result<(), HaProxyErr> {
|
||||||
let cert_path = cert_path.as_ref();
|
let cert_path = cert_path.as_ref();
|
||||||
let cert_name = cert_path.file_name().unwrap().to_string_lossy().to_string();
|
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();
|
let cert_content = tokio::fs::read_to_string(cert_path).await.unwrap();
|
||||||
|
|||||||
20
src/main.rs
20
src/main.rs
@ -3,7 +3,7 @@ use log::{debug, error, info};
|
|||||||
use crate::{
|
use crate::{
|
||||||
certs::{load_cert_from_fullchain, AcmeApiEndpoint, CertExt, CertRequester},
|
certs::{load_cert_from_fullchain, AcmeApiEndpoint, CertExt, CertRequester},
|
||||||
config::Config,
|
config::Config,
|
||||||
http::ChallengeServer, haproxy::HaProxyApi,
|
http::{ChallengeServer, ChallengeManager}, haproxy::HaProxyApi,
|
||||||
};
|
};
|
||||||
|
|
||||||
mod certs;
|
mod certs;
|
||||||
@ -32,7 +32,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
|
|
||||||
debug!("Config file: {:#?}", &conf);
|
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
|
// Create the http server for serving the challenges
|
||||||
let srv = ChallengeServer::new(&conf.http.ip, conf.http.port)?;
|
let srv = ChallengeServer::new(&conf.http.ip, conf.http.port)?;
|
||||||
@ -44,8 +44,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
srv.start().await.unwrap().0.await;
|
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<HaProxyApi>) {
|
||||||
// See what certs are requested in the config file
|
// 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
|
// Check if the cert needs to be created / renewed
|
||||||
let should_renew = load_cert_from_fullchain(&conf.fullchain_file)
|
let should_renew = load_cert_from_fullchain(&conf.fullchain_file)
|
||||||
.await
|
.await
|
||||||
@ -70,7 +78,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
info!("Certificate {name} needs to be renewed. Using endpoint: {endpoint}");
|
info!("Certificate {name} needs to be renewed. Using endpoint: {endpoint}");
|
||||||
|
|
||||||
let requester = CertRequester::new(endpoint, conf.clone(), mgr.clone());
|
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)) => {
|
(Ok(_), Some(api)) => {
|
||||||
match api.update_cert(&conf.fullchain_file).await {
|
match api.update_cert(&conf.fullchain_file).await {
|
||||||
Ok(()) => info!("Certificate update in haproxy completed"),
|
Ok(()) => info!("Certificate update in haproxy completed"),
|
||||||
@ -84,8 +92,4 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
info!("Certificate {name} does not need to be renewed");
|
info!("Certificate {name} does not need to be renewed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
info!("All done. Shutting down");
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user