feat: change default singleton port and support to change the port

This commit is contained in:
GyDi 2022-09-06 00:45:01 +08:00
parent 9e7c7ac163
commit c058c29755
No known key found for this signature in database
GPG Key ID: 1C95E0D3467B3084
4 changed files with 36 additions and 24 deletions

View File

@ -5,6 +5,10 @@ use serde::{Deserialize, Serialize};
/// ### `verge.yaml` schema /// ### `verge.yaml` schema
#[derive(Default, Debug, Clone, Deserialize, Serialize)] #[derive(Default, Debug, Clone, Deserialize, Serialize)]
pub struct Verge { pub struct Verge {
/// app listening port
/// for app singleton
pub app_singleton_port: Option<u16>,
// i18n // i18n
pub language: Option<String>, pub language: Option<String>,

View File

@ -17,15 +17,24 @@ use tauri::{
}; };
fn main() -> std::io::Result<()> { 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"); println!("app exists");
return Ok(()); 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")] #[cfg(target_os = "windows")]
unsafe { unsafe {
use crate::utils::dirs; use crate::utils::dirs;
dirs::init_portable_flag(); dirs::init_portable_flag();
} }
@ -167,13 +176,6 @@ fn main() -> std::io::Result<()> {
builder = builder.menu(Menu::new().add_submenu(submenu_file)); 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 builder
.build(context) .build(context)
.expect("error while running tauri application") .expect("error while running tauri application")

View File

@ -3,9 +3,6 @@ use tauri::{App, AppHandle, Manager};
/// handle something when start app /// handle something when start app
pub fn resolve_setup(app: &App) { pub fn resolve_setup(app: &App) {
// setup a simple http server for singleton
server::embed_server(&app.handle());
// init app config // init app config
init::init_app(app.package_info()); init::init_app(app.package_info());
@ -13,6 +10,14 @@ pub fn resolve_setup(app: &App) {
// should be initialized after init_app fix #122 // should be initialized after init_app fix #122
let core = Core::new(); 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.set_win(app.get_window("main"));
core.init(app.app_handle()); core.init(app.app_handle());

View File

@ -1,19 +1,22 @@
extern crate warp; extern crate warp;
use super::resolve;
use port_scanner::local_port_available; use port_scanner::local_port_available;
use tauri::{AppHandle, Manager}; use tauri::AppHandle;
use warp::Filter; use warp::Filter;
#[cfg(not(feature = "verge-dev"))] #[cfg(not(feature = "verge-dev"))]
const SERVER_PORT: u16 = 33333; const SERVER_PORT: u16 = 33331;
#[cfg(feature = "verge-dev")] #[cfg(feature = "verge-dev")]
const SERVER_PORT: u16 = 11233; const SERVER_PORT: u16 = 11233;
/// check whether there is already exists /// check whether there is already exists
pub fn check_singleton() -> Result<(), ()> { pub fn check_singleton(port: Option<u16>) -> Result<(), ()> {
if !local_port_available(SERVER_PORT) { let port = port.unwrap_or(SERVER_PORT);
if !local_port_available(port) {
tauri::async_runtime::block_on(async { 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(); reqwest::get(url).await.unwrap();
Err(()) Err(())
}) })
@ -24,18 +27,16 @@ pub fn check_singleton() -> Result<(), ()> {
/// The embed server only be used to implement singleton process /// The embed server only be used to implement singleton process
/// maybe it can be used as pac server later /// maybe it can be used as pac server later
pub fn embed_server(app: &AppHandle) { pub fn embed_server(app_handle: &AppHandle, port: Option<u16>) {
let window = app.get_window("main").unwrap(); let app_handle = app_handle.clone();
let port = port.unwrap_or(SERVER_PORT);
tauri::async_runtime::spawn(async move { tauri::async_runtime::spawn(async move {
let commands = warp::path!("commands" / "visible").map(move || { let commands = warp::path!("commands" / "visible").map(move || {
window.show().unwrap(); resolve::create_window(&app_handle);
window.set_focus().unwrap();
return format!("ok"); return format!("ok");
}); });
warp::serve(commands) warp::serve(commands).bind(([127, 0, 0, 1], port)).await;
.bind(([127, 0, 0, 1], SERVER_PORT))
.await;
}); });
} }