refactor: 重构项目架构与模块化设计

- 调整项目架构描述,明确 Tauri 仅提供原生桌面能力,业务逻辑由 Nitro Server 处理,并重构模块组织结构,将命令、Sidecar 进程管理等逻辑分离至独立模块。
- 移除未使用的 serde_json 依赖项
- 移除未使用的 serde_json 依赖项
- 添加原生桌面功能命令模块,包含用于从 Rust 向前端返回问候消息的示例命令。
- 重构代码结构,将核心功能拆分为独立模块,统一管理 Nitro 进程的启动与清理,并通过模块化方式提升可维护性。
- 添加 Nitro Sidecar 进程管理功能,自动查找可用端口、启动服务器、监听启动信号并创建主窗口,同时在应用退出时安全清理进程。
This commit is contained in:
2026-01-16 21:21:45 +08:00
parent fc90a29c03
commit 5ee6c148c7
6 changed files with 152 additions and 112 deletions

8
src/commands/mod.rs Normal file
View File

@@ -0,0 +1,8 @@
// 原生桌面功能命令
// 未来可能包含: 文件对话框、系统通知、剪贴板等
// 示例命令 (可根据需要删除或替换)
#[tauri::command]
pub fn greet(name: &str) -> String {
format!("Hello, {}! You've been greeted from Rust!", name)
}

View File

@@ -1,34 +1,10 @@
use std::sync::Mutex;
use std::time::Duration;
use tauri::Manager;
use tauri_plugin_shell::process::{CommandChild, CommandEvent};
use tauri_plugin_shell::ShellExt;
// 全局状态:存储 Nitro 进程句柄
struct NitroProcess(Mutex<Option<CommandChild>>);
// 模块声明
mod commands;
mod sidecar;
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
#[tauri::command]
fn greet(name: &str) -> String {
format!("Hello, {}! You've been greeted from Rust!", name)
}
// 检查端口是否可用
async fn is_port_available(port: u16) -> bool {
tokio::net::TcpListener::bind(format!("127.0.0.1:{}", port))
.await
.is_ok()
}
// 查找可用端口
async fn find_available_port(start: u16) -> u16 {
for port in start..start + 100 {
if is_port_available(port).await {
return port;
}
}
start // 回退到起始端口
}
use sidecar::NitroProcess;
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
@@ -36,94 +12,23 @@ pub fn run() {
.plugin(tauri_plugin_opener::init())
.plugin(tauri_plugin_shell::init())
.setup(|app| {
// 使用 Tauri 的状态管理存储进程句柄
app.manage(NitroProcess(Mutex::new(None)));
// 注册全局状态
app.manage(NitroProcess(std::sync::Mutex::new(None)));
// 启动 Nitro Sidecar 进程
let app_handle = app.handle().clone();
// 异步启动 Nitro sidecar
tauri::async_runtime::spawn(async move {
// 查找可用端口
let port = find_available_port(3000).await;
println!("使用端口: {}", port);
// 启动 sidecar
let sidecar = app_handle
.shell()
.sidecar("nitro-server")
.expect("无法找到 nitro-server sidecar")
.env("NITRO_PORT", port.to_string());
let (mut rx, child) = sidecar.spawn().expect("启动 sidecar 失败");
// 保存进程句柄到全局状态
if let Some(state) = app_handle.try_state::<NitroProcess>() {
*state.0.lock().unwrap() = Some(child);
}
// 监听 stdout等待服务器就绪信号
let start_time = std::time::Instant::now();
let timeout = Duration::from_secs(5);
let mut server_ready = false;
while let Some(event) = rx.recv().await {
if let CommandEvent::Stdout(line) = event {
let output = String::from_utf8_lossy(&line);
println!("Nitro: {}", output);
// 检测服务器启动成功的标志
if output.contains("Listening on:") || output.contains("localhost") {
server_ready = true;
println!("✓ Nitro 服务器启动成功!");
// 创建主窗口
let url = format!("http://localhost:{}", port);
tauri::WebviewWindowBuilder::new(
&app_handle,
"main",
tauri::WebviewUrl::External(url.parse().unwrap()),
)
.title("Nitro Application")
.inner_size(1200.0, 800.0)
.center()
.build()
.expect("创建窗口失败");
break;
}
}
// 超时检查
if start_time.elapsed() > timeout {
eprintln!("✗ 启动超时Nitro 服务器未能在 5 秒内启动");
break;
}
}
if !server_ready {
eprintln!("✗ Nitro 服务器启动失败");
std::process::exit(1);
}
});
sidecar::spawn_nitro_sidecar(app_handle);
Ok(())
})
.invoke_handler(tauri::generate_handler![greet])
.invoke_handler(tauri::generate_handler![commands::greet])
.build(tauri::generate_context!())
.expect("error while building tauri application")
.run(|app_handle, event| {
// 监听应用退出事件,清理 Nitro 进程
match event {
tauri::RunEvent::ExitRequested { .. } | tauri::RunEvent::Exit => {
println!("应用退出,正在清理 Nitro 进程...");
if let Some(state) = app_handle.try_state::<NitroProcess>() {
if let Ok(mut process) = state.0.lock() {
if let Some(child) = process.take() {
let _ = child.kill();
println!("✓ Nitro 进程已终止");
}
}
}
sidecar::cleanup_nitro_process(app_handle);
}
_ => {}
}

106
src/sidecar.rs Normal file
View File

@@ -0,0 +1,106 @@
use std::sync::Mutex;
use std::time::Duration;
use tauri::Manager;
use tauri_plugin_shell::process::{CommandChild, CommandEvent};
use tauri_plugin_shell::ShellExt;
// 全局状态:存储 Nitro 进程句柄
pub struct NitroProcess(pub Mutex<Option<CommandChild>>);
// 检查端口是否可用
async fn is_port_available(port: u16) -> bool {
tokio::net::TcpListener::bind(format!("127.0.0.1:{}", port))
.await
.is_ok()
}
// 查找可用端口
async fn find_available_port(start: u16) -> u16 {
for port in start..start + 100 {
if is_port_available(port).await {
return port;
}
}
start // 回退到起始端口
}
/// 启动 Nitro Sidecar 进程并创建主窗口
pub fn spawn_nitro_sidecar(app_handle: tauri::AppHandle) {
tauri::async_runtime::spawn(async move {
// 查找可用端口
let port = find_available_port(3000).await;
println!("使用端口: {}", port);
// 启动 sidecar
let sidecar = app_handle
.shell()
.sidecar("nitro-server")
.expect("无法找到 nitro-server sidecar")
.env("NITRO_PORT", port.to_string());
let (mut rx, child) = sidecar.spawn().expect("启动 sidecar 失败");
// 保存进程句柄到全局状态
if let Some(state) = app_handle.try_state::<NitroProcess>() {
*state.0.lock().unwrap() = Some(child);
}
// 监听 stdout等待服务器就绪信号
let start_time = std::time::Instant::now();
let timeout = Duration::from_secs(5);
let mut server_ready = false;
while let Some(event) = rx.recv().await {
if let CommandEvent::Stdout(line) = event {
let output = String::from_utf8_lossy(&line);
println!("Nitro: {}", output);
// 检测服务器启动成功的标志
if output.contains("Listening on:") || output.contains("localhost") {
server_ready = true;
println!("✓ Nitro 服务器启动成功!");
// 创建主窗口
let url = format!("http://localhost:{}", port);
tauri::WebviewWindowBuilder::new(
&app_handle,
"main",
tauri::WebviewUrl::External(url.parse().unwrap()),
)
.title("Nitro Application")
.inner_size(1200.0, 800.0)
.center()
.build()
.expect("创建窗口失败");
break;
}
}
// 超时检查
if start_time.elapsed() > timeout {
eprintln!("✗ 启动超时Nitro 服务器未能在 5 秒内启动");
break;
}
}
if !server_ready {
eprintln!("✗ Nitro 服务器启动失败");
std::process::exit(1);
}
});
}
/// 清理 Nitro 进程 (在应用退出时调用)
pub fn cleanup_nitro_process(app_handle: &tauri::AppHandle) {
println!("应用退出,正在清理 Nitro 进程...");
if let Some(state) = app_handle.try_state::<NitroProcess>() {
if let Ok(mut process) = state.0.lock() {
if let Some(child) = process.take() {
let _ = child.kill();
println!("✓ Nitro 进程已终止");
}
}
}
}