use axum_server::tls_rustls::RustlsConfig;
use futures::future::BoxFuture;
pub struct TlsConfig {
cert_path: std::path::PathBuf,
key_path: std::path::PathBuf,
}
impl TlsConfig {
pub fn new(
cert_path: std::path::PathBuf,
key_path: std::path::PathBuf,
) -> Self {
Self {
cert_path,
key_path,
}
}
pub async fn create_tls_config(&self) -> std::io::Result<RustlsConfig> {
RustlsConfig::from_pem_file(
self.cert_path.clone(),
self.key_path.clone(),
)
.await
}
pub fn reload_task(self, config: RustlsConfig) -> BoxFuture<'static, ()> {
const ONE_HOUR: std::time::Duration =
std::time::Duration::from_secs(60 * 60);
Box::pin(async move {
loop {
tokio::time::sleep(ONE_HOUR).await;
if let Err(e) = config
.reload_from_pem_file(
self.cert_path.clone(),
self.key_path.clone(),
)
.await
{
tracing::error!(
"failed to reload rustls configuration: {}",
e
);
} else {
tracing::info!("rustls configuration reloaded");
}
}
})
}
}