add tauri-local-host plugin for sw (#345)

This commit is contained in:
Krishan 2024-09-13 22:04:35 +10:00 committed by GitHub
parent db5a2dbe01
commit aef6cf730b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 50 additions and 1 deletions

37
src-tauri/Cargo.lock generated
View file

@ -82,6 +82,12 @@ dependencies = [
"x11rb", "x11rb",
] ]
[[package]]
name = "ascii"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d92bec98840b8f03a5ff5413de5293bfcd8bf96467cf5452609f939ec6f5de16"
[[package]] [[package]]
name = "async-broadcast" name = "async-broadcast"
version = "0.5.1" version = "0.5.1"
@ -442,6 +448,12 @@ dependencies = [
"windows-targets 0.48.5", "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]] [[package]]
name = "cinny" name = "cinny"
version = "4.2.0" version = "4.2.0"
@ -450,6 +462,7 @@ dependencies = [
"serde_json", "serde_json",
"tauri", "tauri",
"tauri-build", "tauri-build",
"tauri-plugin-localhost",
] ]
[[package]] [[package]]
@ -3595,6 +3608,17 @@ dependencies = [
"tauri-utils", "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]] [[package]]
name = "tauri-runtime" name = "tauri-runtime"
version = "0.14.4" version = "0.14.4"
@ -3786,6 +3810,19 @@ dependencies = [
"time-core", "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]] [[package]]
name = "tinyvec" name = "tinyvec"
version = "1.6.0" version = "1.6.0"

View file

@ -18,6 +18,7 @@ tauri-build = { version = "1.5.3", features = [] }
serde_json = "1.0.109" serde_json = "1.0.109"
serde = { version = "1.0.193", features = ["derive"] } serde = { version = "1.0.193", features = ["derive"] }
tauri = { version = "1.7.1", features = ["api-all", "devtools", "updater"] } tauri = { version = "1.7.1", features = ["api-all", "devtools", "updater"] }
tauri-plugin-localhost = "0.1.0"
[features] [features]
# by default Tauri runs in production mode # by default Tauri runs in production mode

View file

@ -6,13 +6,24 @@
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
mod menu; mod menu;
use tauri::{utils::config::AppUrl, WindowUrl};
fn main() { 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(); let builder = tauri::Builder::default();
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
let builder = builder.menu(menu::menu()); let builder = builder.menu(menu::menu());
builder builder
.run(tauri::generate_context!()) .plugin(tauri_plugin_localhost::Builder::new(port).build())
.run(context)
.expect("error while building tauri application") .expect("error while building tauri application")
} }