Skip to content

Commit

Permalink
feat:panic is port is not available for use
Browse files Browse the repository at this point in the history
  • Loading branch information
0xbrayo committed Jan 20, 2025
1 parent fe4cbe9 commit 9b91b01
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
33 changes: 32 additions & 1 deletion src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<bool> {
let addr = format!("127.0.0.1:{}", port)
.parse::<SocketAddr>()
.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")
}
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ fn handle(rx: Receiver<ModuleMessage>, state: Arc<Mutex<ManagerState>>) {
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 {
Expand Down

0 comments on commit 9b91b01

Please sign in to comment.