diff --git a/Cargo.lock b/Cargo.lock index 745fa3505..f4dc5ba6c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -502,6 +502,16 @@ dependencies = [ "typenum", ] +[[package]] +name = "ctrlc" +version = "3.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d91974fbbe88ec1df0c24a4f00f99583667a7e2e6272b2b92d294d81e462173" +dependencies = [ + "nix", + "winapi", +] + [[package]] name = "curl" version = "0.4.44" @@ -610,6 +620,7 @@ dependencies = [ "clap", "colored 2.0.0", "convert_case", + "ctrlc", "dioxus", "dioxus-rsx-interpreter", "dirs 4.0.0", @@ -1621,6 +1632,18 @@ dependencies = [ "tempfile", ] +[[package]] +name = "nix" +version = "0.25.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e322c04a9e3440c327fca7b6c8a63e6890a32fa2ad689db972425f07e0d22abb" +dependencies = [ + "autocfg", + "bitflags", + "cfg-if", + "libc", +] + [[package]] name = "notify" version = "5.0.0-pre.16" diff --git a/Cargo.toml b/Cargo.toml index 89558b451..6d707cfab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -58,10 +58,11 @@ dioxus = { git = "https://github.com/dioxuslabs/dioxus/", features = ["hot-reloa dioxus-rsx-interpreter = { git = "https://github.com/dioxuslabs/dioxus/" } proc-macro2 = { version = "1.0", features = ["span-locations"] } +lazy_static = "1.4.0" # plugin packages mlua = { version = "0.8.1", features = ["lua54", "vendored", "async", "send"] } -lazy_static = "1.4.0" +ctrlc = "3.2.3" [[bin]] path = "src/main.rs" diff --git a/src/main.rs b/src/main.rs index 500e3a2ac..e60606fb6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,7 @@ async fn main() -> Result<()> { available: true, required: vec![], }); + if !plugin_state { log::error!("🚫 Plugin system initialization failed"); exit(1); diff --git a/src/server/mod.rs b/src/server/mod.rs index 5abc29ae4..ebb328ad0 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -53,6 +53,13 @@ struct WsReloadState { } pub async fn startup(port: u16, config: CrateConfig) -> Result<()> { + // ctrl-c shutdown checker + let crate_config = config.clone(); + let _ = ctrlc::set_handler(move || { + let _ = PluginManager::on_serve_shutdown(&crate_config); + std::process::exit(0); + }); + if config.hot_reload { startup_hot_reload(port, config).await? } else { @@ -61,10 +68,7 @@ pub async fn startup(port: u16, config: CrateConfig) -> Result<()> { Ok(()) } -pub async fn startup_hot_reload( - port: u16, - config: CrateConfig, -) -> Result<()> { +pub async fn startup_hot_reload(port: u16, config: CrateConfig) -> Result<()> { let first_build_result = crate::builder::build(&config, false)?; log::info!("🚀 Starting development server..."); @@ -279,10 +283,7 @@ pub async fn startup_hot_reload( Ok(()) } -pub async fn startup_default( - port: u16, - config: CrateConfig, -) -> Result<()> { +pub async fn startup_default(port: u16, config: CrateConfig) -> Result<()> { let first_build_result = crate::builder::build(&config, false)?; log::info!("🚀 Starting development server...");