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 authored and GitHub committed 2023-08-17 07:58:56 -05:00
1 parent 8946944d68
commit fa39408d58
1 file changed
+20 -19
+20 -19
View File
@@ -5,7 +5,7 @@ use crate::{
output::{print_console_info, PrettierOptions, WebServerInfo}, output::{print_console_info, PrettierOptions, WebServerInfo},
setup_file_watcher, setup_file_watcher_hot_reload, setup_file_watcher, setup_file_watcher_hot_reload,
}, },
BuildResult, CrateConfig, Result, BuildResult, CrateConfig, Result, WebHttpsConfig,
}; };
use axum::{ use axum::{
body::{Full, HttpBody}, body::{Full, HttpBody},
@@ -218,9 +218,20 @@ async fn get_rustls(config: &CrateConfig) -> Result<Option<RustlsConfig>> {
return Ok(None); 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 // 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 // Get paths to store certs, otherwise use ssl/item.pem
let key_path = web_config let key_path = web_config
.key_path .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 // get paths to cert & key
if let (Some(key), Some(cert)) = if let (Some(key), Some(cert)) = (web_config.key_path.clone(), web_config.cert_path.clone()) {
(web_config.key_path.clone(), web_config.cert_path.clone()) Ok((cert, key))
{
(cert, key)
} else { } else {
// missing cert or key // 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 /// Sets up and returns a router
async fn setup_router( async fn setup_router(