feat: 添加启动加载页与Nitro侧车服务自动启动功能
- 添加启动加载页面,包含旋转动画和动态点状提示效果。 - 忽略 binaries 目录下所有文件,但保留 .gitignore 文件本身。 - 添加对二进制文件 binaries/nitro-server 的执行权限允许,启用侧车模式。 - 添加对 shell 插件和相关依赖的支持,包括 os_pipe、shared_child 和 signal-hook 等库,以增强 Tauri 应用的子进程管理与系统交互能力。 - 添加 shell 插件和异步网络支持以增强应用功能 - 添加 Nitro sidecar 服务自动启动功能,包括端口检测、服务器就绪监听及超时处理,并在启动成功后自动创建主窗口。 - 移除窗口配置并添加外部二进制文件路径配置
This commit is contained in:
@@ -1,13 +1,99 @@
|
||||
use tauri_plugin_shell::ShellExt;
|
||||
use tauri_plugin_shell::process::CommandEvent;
|
||||
use std::time::Duration;
|
||||
|
||||
// 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| {
|
||||
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, mut _child) = sidecar.spawn().expect("启动 sidecar 失败");
|
||||
|
||||
// 监听 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)
|
||||
.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])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
|
||||
Reference in New Issue
Block a user