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

41 lines
1.0 KiB
Rust
Raw Normal View History

extern crate warp;
use port_scanner::local_port_available;
2021-12-29 13:49:38 +03:00
use std::sync::{Arc, Mutex};
use tauri::{AppHandle, Manager};
use warp::Filter;
const SERVER_PORT: u16 = 33333;
/// check whether there is already exists
pub fn check_singleton() -> Result<(), ()> {
if !local_port_available(SERVER_PORT) {
tauri::async_runtime::block_on(async {
let url = format!("http://127.0.0.1:{}/commands/visible", SERVER_PORT);
reqwest::get(url).await.unwrap();
Err(())
})
} else {
Ok(())
}
}
/// The embed server only be used to implement singleton process
/// maybe it can be used as pac server later
pub fn embed_server(app: &AppHandle) {
2021-12-29 13:49:38 +03:00
let window = Arc::new(Mutex::new(app.get_window("main").unwrap()));
tauri::async_runtime::spawn(async move {
2021-12-29 13:49:38 +03:00
let commands = warp::path!("commands" / "visible").map(move || {
let win = window.lock().unwrap();
win.show().unwrap();
win.set_focus().unwrap();
return format!("ok");
});
warp::serve(commands)
.bind(([127, 0, 0, 1], SERVER_PORT))
.await;
});
}