feat: change default singleton port and support to change the port

This commit is contained in:
GyDi 2022-09-06 00:45:01 +08:00
parent 9e7c7ac163
commit c058c29755
No known key found for this signature in database
GPG Key ID: 1C95E0D3467B3084
4 changed files with 36 additions and 24 deletions

View File

@ -5,6 +5,10 @@ use serde::{Deserialize, Serialize};
/// ### `verge.yaml` schema
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
pub struct Verge {
/// app listening port
/// for app singleton
pub app_singleton_port: Option<u16>,
// i18n
pub language: Option<String>,

View File

@ -17,15 +17,24 @@ use tauri::{
};
fn main() -> std::io::Result<()> {
if server::check_singleton().is_err() {
let mut context = tauri::generate_context!();
let verge = Verge::new();
if server::check_singleton(verge.app_singleton_port).is_err() {
println!("app exists");
return Ok(());
}
for win in context.config_mut().tauri.windows.iter_mut() {
if verge.enable_silent_start.unwrap_or(false) {
win.visible = false;
}
}
#[cfg(target_os = "windows")]
unsafe {
use crate::utils::dirs;
dirs::init_portable_flag();
}
@ -167,13 +176,6 @@ fn main() -> std::io::Result<()> {
builder = builder.menu(Menu::new().add_submenu(submenu_file));
}
let mut context = tauri::generate_context!();
let verge = Verge::new();
for win in context.config_mut().tauri.windows.iter_mut() {
if verge.enable_silent_start.unwrap_or(false) {
win.visible = false;
}
}
builder
.build(context)
.expect("error while running tauri application")

View File

@ -3,9 +3,6 @@ use tauri::{App, AppHandle, Manager};
/// handle something when start app
pub fn resolve_setup(app: &App) {
// setup a simple http server for singleton
server::embed_server(&app.handle());
// init app config
init::init_app(app.package_info());
@ -13,6 +10,14 @@ pub fn resolve_setup(app: &App) {
// should be initialized after init_app fix #122
let core = Core::new();
{
let verge = core.verge.lock();
let singleton = verge.app_singleton_port.clone();
// setup a simple http server for singleton
server::embed_server(&app.handle(), singleton);
}
core.set_win(app.get_window("main"));
core.init(app.app_handle());

View File

@ -1,19 +1,22 @@
extern crate warp;
use super::resolve;
use port_scanner::local_port_available;
use tauri::{AppHandle, Manager};
use tauri::AppHandle;
use warp::Filter;
#[cfg(not(feature = "verge-dev"))]
const SERVER_PORT: u16 = 33333;
const SERVER_PORT: u16 = 33331;
#[cfg(feature = "verge-dev")]
const SERVER_PORT: u16 = 11233;
/// check whether there is already exists
pub fn check_singleton() -> Result<(), ()> {
if !local_port_available(SERVER_PORT) {
pub fn check_singleton(port: Option<u16>) -> Result<(), ()> {
let port = port.unwrap_or(SERVER_PORT);
if !local_port_available(port) {
tauri::async_runtime::block_on(async {
let url = format!("http://127.0.0.1:{}/commands/visible", SERVER_PORT);
let url = format!("http://127.0.0.1:{}/commands/visible", port);
reqwest::get(url).await.unwrap();
Err(())
})
@ -24,18 +27,16 @@ pub fn check_singleton() -> Result<(), ()> {
/// 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) {
let window = app.get_window("main").unwrap();
pub fn embed_server(app_handle: &AppHandle, port: Option<u16>) {
let app_handle = app_handle.clone();
let port = port.unwrap_or(SERVER_PORT);
tauri::async_runtime::spawn(async move {
let commands = warp::path!("commands" / "visible").map(move || {
window.show().unwrap();
window.set_focus().unwrap();
resolve::create_window(&app_handle);
return format!("ok");
});
warp::serve(commands)
.bind(([127, 0, 0, 1], SERVER_PORT))
.await;
warp::serve(commands).bind(([127, 0, 0, 1], port)).await;
});
}