2021-12-23 01:35:17 +08:00
|
|
|
extern crate warp;
|
|
|
|
|
2022-09-06 00:45:01 +08:00
|
|
|
use super::resolve;
|
2022-11-17 17:07:13 +08:00
|
|
|
use crate::config::IVerge;
|
2021-12-23 01:35:17 +08:00
|
|
|
use port_scanner::local_port_available;
|
2022-09-06 00:45:01 +08:00
|
|
|
use tauri::AppHandle;
|
2021-12-23 01:35:17 +08:00
|
|
|
use warp::Filter;
|
|
|
|
|
|
|
|
/// check whether there is already exists
|
2022-10-28 01:02:47 +08:00
|
|
|
pub fn check_singleton() -> Result<(), ()> {
|
2022-11-17 17:07:13 +08:00
|
|
|
let port = IVerge::get_singleton_port();
|
2022-09-06 00:45:01 +08:00
|
|
|
|
2022-11-12 11:37:23 +08:00
|
|
|
if !local_port_available(port) {
|
|
|
|
tauri::async_runtime::block_on(async {
|
|
|
|
let url = format!("http://127.0.0.1:{}/commands/visible", port);
|
|
|
|
reqwest::get(url).await.unwrap();
|
|
|
|
Err(())
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-12-23 01:35:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The embed server only be used to implement singleton process
|
|
|
|
/// maybe it can be used as pac server later
|
2022-11-14 01:26:33 +08:00
|
|
|
pub fn embed_server(app_handle: AppHandle) {
|
2022-11-12 11:37:23 +08:00
|
|
|
let app_handle = app_handle.clone();
|
2022-11-17 17:07:13 +08:00
|
|
|
let port = IVerge::get_singleton_port();
|
2021-12-23 01:35:17 +08:00
|
|
|
|
2022-11-12 11:37:23 +08:00
|
|
|
tauri::async_runtime::spawn(async move {
|
|
|
|
let commands = warp::path!("commands" / "visible").map(move || {
|
|
|
|
resolve::create_window(&app_handle);
|
2022-11-14 01:26:33 +08:00
|
|
|
format!("ok")
|
2022-11-12 11:37:23 +08:00
|
|
|
});
|
2021-12-29 18:49:38 +08:00
|
|
|
|
2022-11-12 11:37:23 +08:00
|
|
|
warp::serve(commands).bind(([127, 0, 0, 1], port)).await;
|
|
|
|
});
|
2021-12-23 01:35:17 +08:00
|
|
|
}
|