From aef6cf730b600b11d2ca0a180781988e77e9d3bb Mon Sep 17 00:00:00 2001 From: Krishan <33421343+kfiven@users.noreply.github.com> Date: Fri, 13 Sep 2024 22:04:35 +1000 Subject: [PATCH] add tauri-local-host plugin for sw (#345) --- src-tauri/Cargo.lock | 37 +++++++++++++++++++++++++++++++++++++ src-tauri/Cargo.toml | 1 + src-tauri/src/main.rs | 13 ++++++++++++- 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 74dd418..1ba0ee3 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -82,6 +82,12 @@ dependencies = [ "x11rb", ] +[[package]] +name = "ascii" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d92bec98840b8f03a5ff5413de5293bfcd8bf96467cf5452609f939ec6f5de16" + [[package]] name = "async-broadcast" version = "0.5.1" @@ -442,6 +448,12 @@ dependencies = [ "windows-targets 0.48.5", ] +[[package]] +name = "chunked_transfer" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e4de3bc4ea267985becf712dc6d9eed8b04c953b3fcfb339ebc87acd9804901" + [[package]] name = "cinny" version = "4.2.0" @@ -450,6 +462,7 @@ dependencies = [ "serde_json", "tauri", "tauri-build", + "tauri-plugin-localhost", ] [[package]] @@ -3595,6 +3608,17 @@ dependencies = [ "tauri-utils", ] +[[package]] +name = "tauri-plugin-localhost" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f20786ccff879045f6bafec445fb5c6740c0c057372d2f992ae1281e4658c681" +dependencies = [ + "serde_json", + "tauri", + "tiny_http", +] + [[package]] name = "tauri-runtime" version = "0.14.4" @@ -3786,6 +3810,19 @@ dependencies = [ "time-core", ] +[[package]] +name = "tiny_http" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0d6ef4e10d23c1efb862eecad25c5054429a71958b4eeef85eb5e7170b477ca" +dependencies = [ + "ascii", + "chunked_transfer", + "log", + "time", + "url", +] + [[package]] name = "tinyvec" version = "1.6.0" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 8115c0a..0ef34bd 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -18,6 +18,7 @@ tauri-build = { version = "1.5.3", features = [] } serde_json = "1.0.109" serde = { version = "1.0.193", features = ["derive"] } tauri = { version = "1.7.1", features = ["api-all", "devtools", "updater"] } +tauri-plugin-localhost = "0.1.0" [features] # by default Tauri runs in production mode diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 56a1ab7..e079c39 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -6,13 +6,24 @@ #[cfg(target_os = "macos")] mod menu; +use tauri::{utils::config::AppUrl, WindowUrl}; + fn main() { + let port = 44548; + + let mut context = tauri::generate_context!(); + let url = format!("http://localhost:{}", port).parse().unwrap(); + let window_url = WindowUrl::External(url); + // rewrite the config so the IPC is enabled on this URL + context.config_mut().build.dist_dir = AppUrl::Url(window_url.clone()); + context.config_mut().build.dev_path = AppUrl::Url(window_url.clone()); let builder = tauri::Builder::default(); #[cfg(target_os = "macos")] let builder = builder.menu(menu::menu()); builder - .run(tauri::generate_context!()) + .plugin(tauri_plugin_localhost::Builder::new(port).build()) + .run(context) .expect("error while building tauri application") }