feat(registry): 迁移至 Rust + Axum + SQLite 并新增编辑、分页和档案同步功能

- 后端从 Node.js 迁移到 Rust,使用 Axum 框架和 SQLite 存储
- 新增档案编辑对话框,支持修改机型、登记人及替换/删除照片
- 档案列表增加分页导航(每页20条)
- 新增档案同步状态提示及手动重试同步按钮
- Node.js 原版后端及网页保留为回退方案,移至 legacy-web 目录
- 更新 .gitignore 添加 Rust 构建产物(target、dist)
- 更新 README 文档,补充 Rust 启动、开发、检查及打包说明
- 启动源码版.cmd 默认转发至 Rust 版启动脚本
```
This commit is contained in:
2026-10-21 11:54:38 +08:00
parent 85924f35ff
commit 4d55796cb3
39 changed files with 5925 additions and 39 deletions
+74
View File
@@ -0,0 +1,74 @@
mod archive;
mod error;
mod http;
mod store;
#[cfg(test)]
mod tests;
use std::{path::PathBuf, sync::mpsc};
type Job = Box<dyn FnOnce(&mut store::Store) + Send>;
#[derive(Clone)]
pub struct Worker(mpsc::Sender<Job>);
impl Worker {
async fn run<T: Send + 'static>(
&self,
f: impl FnOnce(&mut store::Store) -> error::Result<T> + Send + 'static,
) -> error::Result<T> {
let (tx, rx) = tokio::sync::oneshot::channel();
self.0
.send(Box::new(move |s| {
let _ = tx.send(f(s));
}))
.map_err(|_| "数据库线程已停止".to_string())?;
rx.await
.map_err(|_| error::AppError::Internal("数据库线程已停止".into()))?
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut args = std::env::args().skip(1);
let root = match args.next().as_deref() {
Some("--data-root") => PathBuf::from(args.next().ok_or("缺少数据目录")?),
None => std::env::current_exe()?
.parent()
.ok_or("无法确定程序目录")?
.to_owned(),
_ => return Err("用法: fpv-rid-registry.exe [--data-root DIRECTORY]".into()),
};
if args.next().is_some() {
return Err("未知启动参数".into());
}
let listener = tokio::net::TcpListener::bind("127.0.0.1:8787")
.await
.map_err(|e| format!("无法监听 8787,请关闭旧版或其他实例:{e}"))?;
std::fs::create_dir_all(root.join("registry/data"))?;
let lock = std::fs::OpenOptions::new()
.create(true)
.truncate(false)
.read(true)
.write(true)
.open(root.join("registry/data/rust.lock"))?;
fs2::FileExt::try_lock_exclusive(&lock).map_err(|_| "该数据目录已被另一个 Rust 实例使用")?;
let store = store::Store::open(root)?;
let (tx, rx) = mpsc::channel::<Job>();
let worker_thread = std::thread::spawn(move || {
let mut store = store;
if let Err(e) = archive::sync(&mut store) {
eprintln!("档案待同步:{e}");
}
for job in rx {
job(&mut store);
}
});
println!("FPV-RID Rust 登记站:http://127.0.0.1:8787");
axum::serve(listener, http::router(Worker(tx)))
.with_graceful_shutdown(async {
let _ = tokio::signal::ctrl_c().await;
})
.await?;
worker_thread.join().map_err(|_| "数据库线程异常退出")?;
drop(lock);
Ok(())
}