refactor: 重构项目结构与文件命名规范
- 移除已过时的忽略规则并更新 Cargo 和 Tauri 生成文件的忽略配置 - 删除已过时的项目协作指南文件AGENTS.md - 重命名 binaries/.gitignore 文件以保持项目结构一致性 - 重命名构建脚本文件以符合项目根目录的规范 - 删除了包含所有依赖项及其哈希值的锁文件,移除了项目所有构建工具和平台特定二进制文件的详细版本信息。 - 重命名能力配置文件以统一路径结构 - 重命名 Cargo.lock 文件以匹配项目根目录的命名规范 - 重命名 Cargo.toml 文件以统一项目根目录的配置文件路径 - 重命名图标文件以统一项目中的资源路径 - 重命名图标文件以正确反映其在项目中的位置 - 重命名图标文件以正确反映其在项目中的位置 - 重命名图标文件以统一项目中的资源路径结构 - 重命名图标文件路径以保持项目结构一致性 - 重命名图标文件以统一项目资源路径 - 重命名图标文件以正确反映其在项目中的新路径 - 重命名图标文件以正确匹配新路径结构 - 重命名图标文件以正确反映其在项目中的位置 - 重命名图标文件以匹配新路径结构 - 重命名图标文件以正确反映其在项目中的位置 - 重命名图标文件以匹配新的项目路径结构 - 重命名图标文件以正确反映其在项目中的新路径 - 重命名图标文件以正确反映其在项目中的新路径 - 重命名图标文件以正确反映其在项目中的位置 - 重命名图标文件以正确反映其在项目中的位置 - 删除项目中的 package.json 文件及其中定义的依赖和脚本配置 - 删除 README.md 文件 - 删除不再需要的构建和生成文件忽略规则 - 重命名源文件路径以符合项目结构规范 - 重命名主源文件路径以符合项目结构规范 - 重命名 tauri.conf.json 文件以统一配置文件路径
This commit is contained in:
131
src/lib.rs
Normal file
131
src/lib.rs
Normal file
@@ -0,0 +1,131 @@
|
||||
use tauri::Manager;
|
||||
use tauri_plugin_shell::ShellExt;
|
||||
use tauri_plugin_shell::process::{CommandEvent, CommandChild};
|
||||
use std::time::Duration;
|
||||
use std::sync::Mutex;
|
||||
|
||||
// 全局状态:存储 Nitro 进程句柄
|
||||
struct NitroProcess(Mutex<Option<CommandChild>>);
|
||||
|
||||
// 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 // 回退到起始端口
|
||||
}
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
.plugin(tauri_plugin_shell::init())
|
||||
.setup(|app| {
|
||||
// 使用 Tauri 的状态管理存储进程句柄
|
||||
app.manage(NitroProcess(Mutex::new(None)));
|
||||
|
||||
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);
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
})
|
||||
.invoke_handler(tauri::generate_handler![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 进程已终止");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
});
|
||||
}
|
||||
6
src/main.rs
Normal file
6
src/main.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||
|
||||
fn main() {
|
||||
tauri_demo_lib::run()
|
||||
}
|
||||
Reference in New Issue
Block a user