From c058c29755419c16df6492bbbacc1e5c26f85bc3 Mon Sep 17 00:00:00 2001 From: GyDi Date: Tue, 6 Sep 2022 00:45:01 +0800 Subject: [PATCH] feat: change default singleton port and support to change the port --- src-tauri/src/core/verge.rs | 4 ++++ src-tauri/src/main.rs | 20 +++++++++++--------- src-tauri/src/utils/resolve.rs | 11 ++++++++--- src-tauri/src/utils/server.rs | 25 +++++++++++++------------ 4 files changed, 36 insertions(+), 24 deletions(-) diff --git a/src-tauri/src/core/verge.rs b/src-tauri/src/core/verge.rs index d206dbb..d3d517a 100644 --- a/src-tauri/src/core/verge.rs +++ b/src-tauri/src/core/verge.rs @@ -5,6 +5,10 @@ use serde::{Deserialize, Serialize}; /// ### `verge.yaml` schema #[derive(Default, Debug, Clone, Deserialize, Serialize)] pub struct Verge { + /// app listening port + /// for app singleton + pub app_singleton_port: Option, + // i18n pub language: Option, diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 542eb7a..a116ffb 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -17,15 +17,24 @@ use tauri::{ }; fn main() -> std::io::Result<()> { - if server::check_singleton().is_err() { + let mut context = tauri::generate_context!(); + + let verge = Verge::new(); + + if server::check_singleton(verge.app_singleton_port).is_err() { println!("app exists"); return Ok(()); } + for win in context.config_mut().tauri.windows.iter_mut() { + if verge.enable_silent_start.unwrap_or(false) { + win.visible = false; + } + } + #[cfg(target_os = "windows")] unsafe { use crate::utils::dirs; - dirs::init_portable_flag(); } @@ -167,13 +176,6 @@ fn main() -> std::io::Result<()> { builder = builder.menu(Menu::new().add_submenu(submenu_file)); } - let mut context = tauri::generate_context!(); - let verge = Verge::new(); - for win in context.config_mut().tauri.windows.iter_mut() { - if verge.enable_silent_start.unwrap_or(false) { - win.visible = false; - } - } builder .build(context) .expect("error while running tauri application") diff --git a/src-tauri/src/utils/resolve.rs b/src-tauri/src/utils/resolve.rs index d403430..9c5b405 100644 --- a/src-tauri/src/utils/resolve.rs +++ b/src-tauri/src/utils/resolve.rs @@ -3,9 +3,6 @@ use tauri::{App, AppHandle, Manager}; /// handle something when start app pub fn resolve_setup(app: &App) { - // setup a simple http server for singleton - server::embed_server(&app.handle()); - // init app config init::init_app(app.package_info()); @@ -13,6 +10,14 @@ pub fn resolve_setup(app: &App) { // should be initialized after init_app fix #122 let core = Core::new(); + { + let verge = core.verge.lock(); + let singleton = verge.app_singleton_port.clone(); + + // setup a simple http server for singleton + server::embed_server(&app.handle(), singleton); + } + core.set_win(app.get_window("main")); core.init(app.app_handle()); diff --git a/src-tauri/src/utils/server.rs b/src-tauri/src/utils/server.rs index 11bd262..4d72e4a 100644 --- a/src-tauri/src/utils/server.rs +++ b/src-tauri/src/utils/server.rs @@ -1,19 +1,22 @@ extern crate warp; +use super::resolve; use port_scanner::local_port_available; -use tauri::{AppHandle, Manager}; +use tauri::AppHandle; use warp::Filter; #[cfg(not(feature = "verge-dev"))] -const SERVER_PORT: u16 = 33333; +const SERVER_PORT: u16 = 33331; #[cfg(feature = "verge-dev")] const SERVER_PORT: u16 = 11233; /// check whether there is already exists -pub fn check_singleton() -> Result<(), ()> { - if !local_port_available(SERVER_PORT) { +pub fn check_singleton(port: Option) -> Result<(), ()> { + let port = port.unwrap_or(SERVER_PORT); + + if !local_port_available(port) { tauri::async_runtime::block_on(async { - let url = format!("http://127.0.0.1:{}/commands/visible", SERVER_PORT); + let url = format!("http://127.0.0.1:{}/commands/visible", port); reqwest::get(url).await.unwrap(); Err(()) }) @@ -24,18 +27,16 @@ pub fn check_singleton() -> Result<(), ()> { /// The embed server only be used to implement singleton process /// maybe it can be used as pac server later -pub fn embed_server(app: &AppHandle) { - let window = app.get_window("main").unwrap(); +pub fn embed_server(app_handle: &AppHandle, port: Option) { + let app_handle = app_handle.clone(); + let port = port.unwrap_or(SERVER_PORT); tauri::async_runtime::spawn(async move { let commands = warp::path!("commands" / "visible").map(move || { - window.show().unwrap(); - window.set_focus().unwrap(); + resolve::create_window(&app_handle); return format!("ok"); }); - warp::serve(commands) - .bind(([127, 0, 0, 1], SERVER_PORT)) - .await; + warp::serve(commands).bind(([127, 0, 0, 1], port)).await; }); }