Files
DPM/src-tauri/src/lib.rs
T

60 lines
2.2 KiB
Rust
Raw Normal View History

// 说明:早期 Rust 内嵌后端(server/storage/models/eventsaxum :10801)已随架构迁移到
// Python 后端(FastAPI :10085)而移除;当前 Rust 侧仅负责窗口与生命周期,
// 外加一个「运行时配置读取」命令(供前端读取 exe 旁 config.json,部署免重打包改地址)。
use std::fs;
use std::path::PathBuf;
use tauri::{AppHandle, Manager};
/// 读取运行时配置(部署用,免重新打包):
/// 1) exe 同目录 config.json / dpm.config.json(最高优先)
/// 2) 应用配置目录 config.json
/// 内容示例:
/// { "api_base": "http://192.168.1.9:10085",
/// "mqtt_url": "ws://192.168.1.3:8083/mqtt",
/// "mqtt_username": "dpm", "mqtt_password": "123456" }
/// 返回 JSON 字符串;未配置返回 null。
#[tauri::command]
fn read_runtime_config(app: AppHandle) -> Option<String> {
let mut candidates: Vec<PathBuf> = Vec::new();
if let Ok(exe) = std::env::current_exe() {
if let Some(dir) = exe.parent() {
candidates.push(dir.join("config.json"));
candidates.push(dir.join("dpm.config.json"));
}
}
if let Ok(dir) = app.path().app_config_dir() {
candidates.push(dir.join("config.json"));
}
for p in candidates {
if p.exists() {
if let Ok(s) = fs::read_to_string(&p) {
let t = s.trim();
if t.starts_with('{') {
return Some(t.to_string());
}
}
}
}
None
}
2026-05-12 22:38:52 +08:00
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_opener::init())
2026-05-14 15:07:41 +08:00
.plugin(tauri_plugin_autostart::init(
tauri_plugin_autostart::MacosLauncher::LaunchAgent,
Some(vec![]),
))
.invoke_handler(tauri::generate_handler![read_runtime_config])
2026-05-14 15:39:12 +08:00
.on_window_event(|window, event| {
if let tauri::WindowEvent::CloseRequested { api, .. } = event {
api.prevent_close();
let _ = window.minimize();
}
})
2026-05-12 22:38:52 +08:00
.run(tauri::generate_context!())
.expect("error while running tauri application");
}