refactor: 统一将 Nitro 相关命名更新为 Sidecar
- 将文档中所有关于 Nitro Server 的表述统一更新为 Sidecar Server,以准确反映当前架构中后端服务的命名和职责。 - 更新进程管理名称与相关函数调用,将 Nitro 相关的组件和操作统一替换为 Sidecar,以反映当前模块的正确命名和功能。 - 将 Nitro 相关的进程管理功能重命名为 Sidecar,更新结构体、函数和日志信息以反映新的命名和功能,确保进程启动、状态管理和清理逻辑正确指向新的 Sidecar 进程。
This commit is contained in:
12
src/lib.rs
12
src/lib.rs
@@ -4,7 +4,7 @@ use tauri::Manager;
|
||||
mod commands;
|
||||
mod sidecar;
|
||||
|
||||
use sidecar::NitroProcess;
|
||||
use sidecar::SidecarProcess;
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
@@ -13,11 +13,11 @@ pub fn run() {
|
||||
.plugin(tauri_plugin_shell::init())
|
||||
.setup(|app| {
|
||||
// 注册全局状态
|
||||
app.manage(NitroProcess(std::sync::Mutex::new(None)));
|
||||
app.manage(SidecarProcess(std::sync::Mutex::new(None)));
|
||||
|
||||
// 启动 Nitro Sidecar 进程
|
||||
// 启动 Sidecar Server 进程
|
||||
let app_handle = app.handle().clone();
|
||||
sidecar::spawn_nitro_sidecar(app_handle);
|
||||
sidecar::spawn_sidecar(app_handle);
|
||||
|
||||
Ok(())
|
||||
})
|
||||
@@ -25,10 +25,10 @@ pub fn run() {
|
||||
.build(tauri::generate_context!())
|
||||
.expect("error while building tauri application")
|
||||
.run(|app_handle, event| {
|
||||
// 监听应用退出事件,清理 Nitro 进程
|
||||
// 监听应用退出事件,清理 Sidecar Server 进程
|
||||
match event {
|
||||
tauri::RunEvent::ExitRequested { .. } | tauri::RunEvent::Exit => {
|
||||
sidecar::cleanup_nitro_process(app_handle);
|
||||
sidecar::cleanup_sidecar_process(app_handle);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@ use tauri::Manager;
|
||||
use tauri_plugin_shell::process::{CommandChild, CommandEvent};
|
||||
use tauri_plugin_shell::ShellExt;
|
||||
|
||||
// 全局状态:存储 Nitro 进程句柄
|
||||
pub struct NitroProcess(pub Mutex<Option<CommandChild>>);
|
||||
// 全局状态:存储 Sidecar 进程句柄
|
||||
pub struct SidecarProcess(pub Mutex<Option<CommandChild>>);
|
||||
|
||||
// 检查端口是否可用
|
||||
async fn is_port_available(port: u16) -> bool {
|
||||
@@ -25,8 +25,8 @@ async fn find_available_port(start: u16) -> u16 {
|
||||
start // 回退到起始端口
|
||||
}
|
||||
|
||||
/// 启动 Nitro Sidecar 进程并创建主窗口
|
||||
pub fn spawn_nitro_sidecar(app_handle: tauri::AppHandle) {
|
||||
/// 启动 Sidecar 进程并创建主窗口
|
||||
pub fn spawn_sidecar(app_handle: tauri::AppHandle) {
|
||||
tauri::async_runtime::spawn(async move {
|
||||
// 查找可用端口
|
||||
let port = find_available_port(3000).await;
|
||||
@@ -35,14 +35,14 @@ pub fn spawn_nitro_sidecar(app_handle: tauri::AppHandle) {
|
||||
// 启动 sidecar
|
||||
let sidecar = app_handle
|
||||
.shell()
|
||||
.sidecar("nitro-server")
|
||||
.expect("无法找到 nitro-server sidecar")
|
||||
.sidecar("server")
|
||||
.expect("无法找到 server")
|
||||
.env("NITRO_PORT", port.to_string());
|
||||
|
||||
let (mut rx, child) = sidecar.spawn().expect("启动 sidecar 失败");
|
||||
|
||||
// 保存进程句柄到全局状态
|
||||
if let Some(state) = app_handle.try_state::<NitroProcess>() {
|
||||
if let Some(state) = app_handle.try_state::<SidecarProcess>() {
|
||||
*state.0.lock().unwrap() = Some(child);
|
||||
}
|
||||
|
||||
@@ -54,12 +54,12 @@ pub fn spawn_nitro_sidecar(app_handle: tauri::AppHandle) {
|
||||
while let Some(event) = rx.recv().await {
|
||||
if let CommandEvent::Stdout(line) = event {
|
||||
let output = String::from_utf8_lossy(&line);
|
||||
println!("Nitro: {}", output);
|
||||
println!("Sidecar: {}", output);
|
||||
|
||||
// 检测服务器启动成功的标志
|
||||
if output.contains("Listening on:") || output.contains("localhost") {
|
||||
server_ready = true;
|
||||
println!("✓ Nitro 服务器启动成功!");
|
||||
println!("✓ Sidecar 启动成功!");
|
||||
|
||||
// 创建主窗口
|
||||
let url = format!("http://localhost:{}", port);
|
||||
@@ -68,7 +68,7 @@ pub fn spawn_nitro_sidecar(app_handle: tauri::AppHandle) {
|
||||
"main",
|
||||
tauri::WebviewUrl::External(url.parse().unwrap()),
|
||||
)
|
||||
.title("Nitro Application")
|
||||
.title("Tauri Application")
|
||||
.inner_size(1200.0, 800.0)
|
||||
.center()
|
||||
.build()
|
||||
@@ -80,26 +80,26 @@ pub fn spawn_nitro_sidecar(app_handle: tauri::AppHandle) {
|
||||
|
||||
// 超时检查
|
||||
if start_time.elapsed() > timeout {
|
||||
eprintln!("✗ 启动超时:Nitro 服务器未能在 5 秒内启动");
|
||||
eprintln!("✗ 启动超时:Sidecar 未能在 5 秒内启动");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if !server_ready {
|
||||
eprintln!("✗ Nitro 服务器启动失败");
|
||||
eprintln!("✗ Sidecar 启动失败");
|
||||
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>() {
|
||||
/// 清理 Sidecar 进程 (在应用退出时调用)
|
||||
pub fn cleanup_sidecar_process(app_handle: &tauri::AppHandle) {
|
||||
println!("应用退出,正在清理 Sidecar 进程...");
|
||||
if let Some(state) = app_handle.try_state::<SidecarProcess>() {
|
||||
if let Ok(mut process) = state.0.lock() {
|
||||
if let Some(child) = process.take() {
|
||||
let _ = child.kill();
|
||||
println!("✓ Nitro 进程已终止");
|
||||
println!("✓ Sidecar 进程已终止");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user