From 9b91b01192676414e684eef95c0791b338a402d6 Mon Sep 17 00:00:00 2001 From: Brayo Date: Mon, 20 Jan 2025 23:10:56 +0300 Subject: [PATCH] feat:panic is port is not available for use --- src-tauri/src/lib.rs | 33 ++++++++++++++++++++++++++++++++- src-tauri/src/manager.rs | 2 +- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index d421be3..ed6107d 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -6,11 +6,13 @@ use lazy_static::lazy_static; use notify::{Config, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher}; use serde::{Deserialize, Serialize}; use std::fs::{create_dir_all, read_to_string, remove_file, write, OpenOptions}; +use std::net::{SocketAddr, TcpListener}; use std::path::{Path, PathBuf}; use std::sync::{mpsc, Condvar, Mutex, OnceLock}; use std::thread; use std::time::Duration; use tauri_plugin_autostart::{MacosLauncher, ManagerExt}; +use tauri_plugin_dialog::{DialogExt, MessageDialogKind}; use tauri_plugin_notification::NotificationExt; mod logging; @@ -84,6 +86,23 @@ pub(crate) fn get_tray_id() -> &'static TrayIconId { &TRAY_ID.get().expect("TRAY_ID not initialized").0 } +pub fn is_port_available(port: u16) -> std::io::Result { + let addr = format!("127.0.0.1:{}", port) + .parse::() + .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidInput, e))?; + + match TcpListener::bind(addr) { + Ok(_) => Ok(true), // Port is available + Err(e) => { + if e.kind() == std::io::ErrorKind::AddrInUse { + Ok(false) // Port is in use + } else { + Err(e) // Other error occurred + } + } + } +} + pub(crate) fn is_first_run() -> &'static bool { FIRST_RUN.get().expect("FIRST_RUN not initialized") } @@ -371,7 +390,19 @@ pub fn run() { asset_resolver: aw_server::endpoints::AssetResolver::new(asset_path_opt), device_id, }; - + if !is_port_available(user_config.defaults.port) + .expect("Failed to check port availability") + { + app.dialog() + .message(format!( + "Port {} is already in use", + user_config.defaults.port + )) + .kind(MessageDialogKind::Error) + .title("Aw-Tauri") + .show(|_| {}); + panic!("Port {} is already in use", user_config.defaults.port); + } tauri::async_runtime::spawn(build_rocket(server_state, aw_config).launch()); let manager_state = manager::start_manager(); diff --git a/src-tauri/src/manager.rs b/src-tauri/src/manager.rs index f3cf979..d7a78aa 100644 --- a/src-tauri/src/manager.rs +++ b/src-tauri/src/manager.rs @@ -252,7 +252,7 @@ fn handle(rx: Receiver, state: Arc>) { app.dialog() .message(format!("{name_clone} crashed. Restarting...")) .kind(MessageDialogKind::Error) - .title("Warning") + .title("Aw-Tauri") .show(|_| {}); error!("Module {name_clone} crashed and is being restarted"); } else {