fix: adjust singleton detect

This commit is contained in:
GyDi 2022-11-18 08:24:27 +08:00
parent 4ae00714d2
commit be81cd72af
No known key found for this signature in database
GPG Key ID: 9C3AD40F1F99880A

View File

@ -2,19 +2,26 @@ extern crate warp;
use super::resolve; use super::resolve;
use crate::config::IVerge; use crate::config::IVerge;
use anyhow::{bail, Result};
use port_scanner::local_port_available; use port_scanner::local_port_available;
use tauri::AppHandle; use tauri::AppHandle;
use warp::Filter; use warp::Filter;
/// check whether there is already exists /// check whether there is already exists
pub fn check_singleton() -> Result<(), ()> { pub fn check_singleton() -> Result<()> {
let port = IVerge::get_singleton_port(); let port = IVerge::get_singleton_port();
if !local_port_available(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", port); let url = format!("http://127.0.0.1:{port}/commands/visible");
reqwest::get(url).await.unwrap(); let resp = reqwest::get(url).await?.text().await?;
Err(())
if &resp == "ok" {
bail!("app exists");
}
log::error!("failed to setup singleton listen server");
Ok(())
}) })
} else { } else {
Ok(()) Ok(())
@ -24,7 +31,6 @@ 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_handle: AppHandle) { pub fn embed_server(app_handle: AppHandle) {
let app_handle = app_handle.clone();
let port = IVerge::get_singleton_port(); let port = IVerge::get_singleton_port();
tauri::async_runtime::spawn(async move { tauri::async_runtime::spawn(async move {