Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: play nice with aw-qt #60

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading