2021-12-22 20:35:17 +03:00
|
|
|
extern crate warp;
|
|
|
|
|
2022-09-05 19:45:01 +03:00
|
|
|
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};
|
2021-12-22 20:35:17 +03:00
|
|
|
use port_scanner::local_port_available;
|
2022-09-05 19:45:01 +03:00
|
|
|
use tauri::AppHandle;
|
2021-12-22 20:35:17 +03:00
|
|
|
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-09-05 19:45:01 +03:00
|
|
|
|
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(())
|
|
|
|
}
|
2021-12-22 20:35:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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();
|
2021-12-22 20:35:17 +03:00
|
|
|
|
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;
|
|
|
|
});
|
2021-12-22 20:35:17 +03:00
|
|
|
}
|