feat: cli use local certificate
This commit is contained in:
1 parent
44c0eb66c5
commit
db93252faf
3 files changed
+93
-15
No files matched your search
@@ -39,6 +39,7 @@ indicatif = "0.17.0-rc.11"
|
|||||||
subprocess = "0.2.9"
|
subprocess = "0.2.9"
|
||||||
|
|
||||||
axum = { version = "0.5.1", features = ["ws", "headers"] }
|
axum = { version = "0.5.1", features = ["ws", "headers"] }
|
||||||
|
axum-server = { version = "0.5.1", features = ["tls-rustls"] }
|
||||||
tower-http = { version = "0.2.2", features = ["full"] }
|
tower-http = { version = "0.2.2", features = ["full"] }
|
||||||
headers = "0.3.7"
|
headers = "0.3.7"
|
||||||
|
|
||||||
|
|||||||
@@ -77,6 +77,12 @@ impl Default for DioxusConfig {
|
|||||||
style: Some(vec![]),
|
style: Some(vec![]),
|
||||||
script: Some(vec![]),
|
script: Some(vec![]),
|
||||||
},
|
},
|
||||||
|
https: WebHttpsConfig {
|
||||||
|
enabled: None,
|
||||||
|
mkcert: None,
|
||||||
|
key_path: None,
|
||||||
|
cert_path: None,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
plugin: toml::Value::Table(toml::map::Map::new()),
|
plugin: toml::Value::Table(toml::map::Map::new()),
|
||||||
}
|
}
|
||||||
@@ -101,6 +107,7 @@ pub struct WebConfig {
|
|||||||
pub proxy: Option<Vec<WebProxyConfig>>,
|
pub proxy: Option<Vec<WebProxyConfig>>,
|
||||||
pub watcher: WebWatcherConfig,
|
pub watcher: WebWatcherConfig,
|
||||||
pub resource: WebResourceConfig,
|
pub resource: WebResourceConfig,
|
||||||
|
pub https: WebHttpsConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
@@ -134,6 +141,14 @@ pub struct WebDevResourceConfig {
|
|||||||
pub script: Option<Vec<PathBuf>>,
|
pub script: Option<Vec<PathBuf>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct WebHttpsConfig {
|
||||||
|
pub enabled: Option<bool>,
|
||||||
|
pub mkcert: Option<bool>,
|
||||||
|
pub key_path: Option<PathBuf>,
|
||||||
|
pub cert_path: Option<PathBuf>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct CrateConfig {
|
pub struct CrateConfig {
|
||||||
pub out_dir: PathBuf,
|
pub out_dir: PathBuf,
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ use axum::{
|
|||||||
routing::{get, get_service},
|
routing::{get, get_service},
|
||||||
Router,
|
Router,
|
||||||
};
|
};
|
||||||
|
use axum_server::tls_rustls::RustlsConfig;
|
||||||
use cargo_metadata::diagnostic::Diagnostic;
|
use cargo_metadata::diagnostic::Diagnostic;
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use dioxus_core::Template;
|
use dioxus_core::Template;
|
||||||
@@ -84,6 +85,34 @@ pub async fn startup(port: u16, config: CrateConfig, start_browser: bool) -> Res
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns an enum of rustls config and a bool if mkcert isn't installed
|
||||||
|
async fn get_rustls(config: &CrateConfig) -> (Option<RustlsConfig>, bool) {
|
||||||
|
let config = &config.dioxus_config.web.https;
|
||||||
|
if config.enabled != Some(true) {
|
||||||
|
return (None, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
match config.mkcert {
|
||||||
|
// mkcert, use it
|
||||||
|
Some(true) => {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
// not mkcert
|
||||||
|
Some(false) => {
|
||||||
|
// get paths to cert & key
|
||||||
|
if let (Some(key), Some(cert)) = (config.key_path.clone(), config.cert_path.clone()) {
|
||||||
|
let rustls = RustlsConfig::from_pem_file(cert, key).await.unwrap();
|
||||||
|
return (Some(rustls), false);
|
||||||
|
} else {
|
||||||
|
// missing cert or key
|
||||||
|
return (None, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// other
|
||||||
|
_ => return (None, false),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct HotReloadState {
|
pub struct HotReloadState {
|
||||||
pub messages: broadcast::Sender<Template<'static>>,
|
pub messages: broadcast::Sender<Template<'static>>,
|
||||||
pub build_manager: Arc<BuildManager>,
|
pub build_manager: Arc<BuildManager>,
|
||||||
@@ -274,6 +303,9 @@ pub async fn startup_hot_reload(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HTTPS
|
||||||
|
let rustls_config = get_rustls(&config).await;
|
||||||
|
|
||||||
// start serve dev-server at 0.0.0.0:8080
|
// start serve dev-server at 0.0.0.0:8080
|
||||||
print_console_info(
|
print_console_info(
|
||||||
&ip,
|
&ip,
|
||||||
@@ -368,13 +400,19 @@ pub async fn startup_hot_reload(
|
|||||||
|
|
||||||
let addr = format!("0.0.0.0:{}", port).parse().unwrap();
|
let addr = format!("0.0.0.0:{}", port).parse().unwrap();
|
||||||
|
|
||||||
let server = axum::Server::bind(&addr).serve(router.into_make_service());
|
|
||||||
|
|
||||||
if start_browser {
|
if start_browser {
|
||||||
let _ = open::that(format!("http://{}", addr));
|
if config.dioxus_config.web.https.enabled == Some(true) {
|
||||||
|
let _ = open::that(format!("https://{}", addr));
|
||||||
|
} else {
|
||||||
|
let _ = open::that(format!("http://{}", addr));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
server.await?;
|
if let (Some(rustls), _) = rustls_config {
|
||||||
|
axum_server::bind_rustls(addr, rustls).serve(router.into_make_service()).await?;
|
||||||
|
} else {
|
||||||
|
axum::Server::bind(&addr).serve(router.into_make_service()).await?;
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -457,6 +495,9 @@ pub async fn startup_default(
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HTTPS
|
||||||
|
let rustls_config = get_rustls(&config).await;
|
||||||
|
|
||||||
// start serve dev-server at 0.0.0.0
|
// start serve dev-server at 0.0.0.0
|
||||||
print_console_info(
|
print_console_info(
|
||||||
&ip,
|
&ip,
|
||||||
@@ -550,13 +591,20 @@ pub async fn startup_default(
|
|||||||
.layer(Extension(ws_reload_state));
|
.layer(Extension(ws_reload_state));
|
||||||
|
|
||||||
let addr = format!("0.0.0.0:{}", port).parse().unwrap();
|
let addr = format!("0.0.0.0:{}", port).parse().unwrap();
|
||||||
let server = axum::Server::bind(&addr).serve(router.into_make_service());
|
|
||||||
|
|
||||||
if start_browser {
|
if start_browser {
|
||||||
let _ = open::that(format!("http://{}", addr));
|
if config.dioxus_config.web.https.enabled == Some(true) {
|
||||||
|
let _ = open::that(format!("https://{}", addr));
|
||||||
|
} else {
|
||||||
|
let _ = open::that(format!("http://{}", addr));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
server.await?;
|
if let (Some(rustls), _) = rustls_config {
|
||||||
|
axum_server::bind_rustls(addr, rustls).serve(router.into_make_service()).await?;
|
||||||
|
} else {
|
||||||
|
axum::Server::bind(&addr).serve(router.into_make_service()).await?;
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -634,14 +682,28 @@ fn print_console_info(ip: &String, port: u16, config: &CrateConfig, options: Pre
|
|||||||
.bold()
|
.bold()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
println!(
|
|
||||||
"\t> Local : {}",
|
if config.dioxus_config.web.https.enabled == Some(true) {
|
||||||
format!("http://localhost:{}/", port).blue()
|
println!(
|
||||||
);
|
"\t> Local : {}",
|
||||||
println!(
|
format!("https://localhost:{}/", port).blue()
|
||||||
"\t> Network : {}",
|
);
|
||||||
format!("http://{}:{}/", ip, port).blue()
|
println!(
|
||||||
);
|
"\t> Network : {}",
|
||||||
|
format!("https://{}:{}/", ip, port).blue()
|
||||||
|
);
|
||||||
|
println!("\t> HTTPS : {}", format!("Enabled").green());
|
||||||
|
} else {
|
||||||
|
println!(
|
||||||
|
"\t> Local : {}",
|
||||||
|
format!("http://localhost:{}/", port).blue()
|
||||||
|
);
|
||||||
|
println!(
|
||||||
|
"\t> Network : {}",
|
||||||
|
format!("http://{}:{}/", ip, port).blue()
|
||||||
|
);
|
||||||
|
println!("\t> HTTPS : {}", format!("Disabled").red());
|
||||||
|
}
|
||||||
println!();
|
println!();
|
||||||
println!("\t> Profile : {}", profile.green());
|
println!("\t> Profile : {}", profile.green());
|
||||||
println!("\t> Hot Reload : {}", hot_reload.cyan());
|
println!("\t> Hot Reload : {}", hot_reload.cyan());
|
||||||
|
|||||||
Reference in new issue
Block a user