clash-verge/src-tauri/src/utils/server.rs

45 lines
1.2 KiB
Rust
Raw Normal View History

extern crate warp;
use super::resolve;
2022-11-17 12:07:13 +03:00
use crate::config::IVerge;
2022-11-18 03:24:27 +03:00
use anyhow::{bail, Result};
use port_scanner::local_port_available;
use tauri::AppHandle;
use warp::Filter;
/// check whether there is already exists
2022-11-18 03:24:27 +03:00
pub fn check_singleton() -> Result<()> {
2022-11-17 12:07:13 +03:00
let port = IVerge::get_singleton_port();
2022-11-12 06:37:23 +03:00
if !local_port_available(port) {
tauri::async_runtime::block_on(async {
2022-11-18 03:24:27 +03:00
let url = format!("http://127.0.0.1:{port}/commands/visible");
let resp = reqwest::get(url).await?.text().await?;
if &resp == "ok" {
bail!("app exists");
}
log::error!("failed to setup singleton listen server");
Ok(())
2022-11-12 06:37:23 +03:00
})
} else {
Ok(())
}
}
/// The embed server only be used to implement singleton process
/// maybe it can be used as pac server later
2022-11-13 20:26:33 +03:00
pub fn embed_server(app_handle: AppHandle) {
2022-11-17 12:07:13 +03:00
let port = IVerge::get_singleton_port();
2022-11-12 06:37:23 +03:00
tauri::async_runtime::spawn(async move {
let commands = warp::path!("commands" / "visible").map(move || {
resolve::create_window(&app_handle);
2022-11-13 20:26:33 +03:00
format!("ok")
2022-11-12 06:37:23 +03:00
});
2021-12-29 13:49:38 +03:00
2022-11-12 06:37:23 +03:00
warp::serve(commands).bind(([127, 0, 0, 1], port)).await;
});
}