If mkcert not enabled, require cert_path and key_path to be specified or error. (#1342)

This commit is contained in:
Vinicius Gobbo Antunes de Oliveira 2023-08-17 14:58:56 +02:00 committed by GitHub
parent 8946944d68
commit fa39408d58
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -5,7 +5,7 @@ use crate::{
output::{print_console_info, PrettierOptions, WebServerInfo},
setup_file_watcher, setup_file_watcher_hot_reload,
},
BuildResult, CrateConfig, Result,
BuildResult, CrateConfig, Result, WebHttpsConfig,
};
use axum::{
body::{Full, HttpBody},
@ -218,9 +218,20 @@ async fn get_rustls(config: &CrateConfig) -> Result<Option<RustlsConfig>> {
return Ok(None);
}
let (cert_path, key_path) = match web_config.mkcert {
let (cert_path, key_path) = if let Some(true) = web_config.mkcert {
// mkcert, use it
Some(true) => {
get_rustls_with_mkcert(web_config)?
} else {
// if mkcert not specified or false, don't use it
get_rustls_without_mkcert(web_config)?
};
Ok(Some(
RustlsConfig::from_pem_file(cert_path, key_path).await?,
))
}
fn get_rustls_with_mkcert(web_config: &WebHttpsConfig) -> Result<(String, String)> {
// Get paths to store certs, otherwise use ssl/item.pem
let key_path = web_config
.key_path
@ -263,28 +274,18 @@ async fn get_rustls(config: &CrateConfig) -> Result<Option<RustlsConfig>> {
}
}
(cert_path, key_path)
Ok((cert_path, key_path))
}
// not mkcert
Some(false) => {
fn get_rustls_without_mkcert(web_config: &WebHttpsConfig) -> Result<(String, String)> {
// get paths to cert & key
if let (Some(key), Some(cert)) =
(web_config.key_path.clone(), web_config.cert_path.clone())
{
(cert, key)
if let (Some(key), Some(cert)) = (web_config.key_path.clone(), web_config.cert_path.clone()) {
Ok((cert, key))
} else {
// missing cert or key
return Err("https is enabled but cert or key path is missing".into());
Err("https is enabled but cert or key path is missing".into())
}
}
// other
_ => return Ok(None),
};
Ok(Some(
RustlsConfig::from_pem_file(cert_path, key_path).await?,
))
}
/// Sets up and returns a router
async fn setup_router(