132 lines
2.9 KiB
Markdown
132 lines
2.9 KiB
Markdown
下面是完整的"健康守护助手"项目,包含所有所需文件。
|
||
|
||
---
|
||
|
||
## 项目结构
|
||
|
||
```
|
||
health-guardian/
|
||
├── Cargo.toml
|
||
├── config.toml
|
||
└── src/
|
||
└── main.rs
|
||
```
|
||
|
||
---
|
||
|
||
## 编译指南
|
||
|
||
### 通用准备
|
||
|
||
```bash
|
||
# 安装 Rust(所有平台通用)
|
||
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
|
||
|
||
# 克隆 / 进入项目目录
|
||
cd health-guardian
|
||
```
|
||
|
||
### macOS
|
||
|
||
```bash
|
||
# 直接编译(原生)
|
||
cargo build --release
|
||
|
||
# 输出位置
|
||
./target/release/health-guardian
|
||
|
||
# 后台运行(终端关闭后仍运行)
|
||
nohup ./target/release/health-guardian &
|
||
|
||
# 或使用 launchd 开机自启(推荐)
|
||
# 创建 ~/Library/LaunchAgents/com.health.guardian.plist
|
||
# 内容见下方 launchd 配置示例
|
||
```
|
||
|
||
### Linux
|
||
|
||
```bash
|
||
# 安装系统依赖(Ubuntu/Debian)
|
||
sudo apt install -y libdbus-1-dev pkg-config
|
||
|
||
# 编译
|
||
cargo build --release
|
||
|
||
# 后台静默运行
|
||
nohup ./target/release/health-guardian > ~/.health-guardian.log 2>&1 &
|
||
|
||
# 或注册为 systemd 用户服务(推荐,开机自启)
|
||
mkdir -p ~/.config/systemd/user/
|
||
cat > ~/.config/systemd/user/health-guardian.service << 'EOF'
|
||
[Unit]
|
||
Description=Health Guardian - Health Reminder Assistant
|
||
|
||
[Service]
|
||
ExecStart=%h/.local/bin/health-guardian
|
||
Restart=on-failure
|
||
|
||
[Install]
|
||
WantedBy=default.target
|
||
EOF
|
||
|
||
cp ./target/release/health-guardian ~/.local/bin/
|
||
systemctl --user enable --now health-guardian
|
||
```
|
||
|
||
### Windows
|
||
|
||
```powershell
|
||
# 确保已安装 Rust(https://rustup.rs)
|
||
# 直接编译(无需额外依赖)
|
||
cargo build --release
|
||
|
||
# 输出位置
|
||
.\target\release\health-guardian.exe
|
||
|
||
# 后台运行(PowerShell,隐藏窗口)
|
||
Start-Process -FilePath ".\target\release\health-guardian.exe" -WindowStyle Hidden
|
||
|
||
# 开机自启:添加到任务计划程序
|
||
schtasks /create /tn "HealthGuardian" /tr "C:\path\to\health-guardian.exe" /sc onlogon /rl HIGHEST /f
|
||
```
|
||
|
||
### 交叉编译(在 macOS/Linux 上编译 Windows 版本)
|
||
|
||
```bash
|
||
# 添加 Windows 目标
|
||
rustup target add x86_64-pc-windows-gnu
|
||
|
||
# 安装交叉编译工具链(Ubuntu)
|
||
sudo apt install gcc-mingw-w64
|
||
|
||
# 交叉编译
|
||
cargo build --release --target x86_64-pc-windows-gnu
|
||
```
|
||
|
||
---
|
||
|
||
## 架构说明与扩展指引
|
||
|
||
**添加新任务类型** 只需在 `config.toml` 中追加一个 `[[tasks]]` 块即可,无需修改代码。例如添加"冥想提醒":
|
||
|
||
```toml
|
||
[[tasks]]
|
||
name = "正念冥想"
|
||
title = "🧘 冥想时间"
|
||
body = "停下手中工作,闭眼做 5 分钟正念呼吸,清空杂念。"
|
||
initial_delay_minutes = 180
|
||
|
||
[tasks.interval]
|
||
unit = "hours"
|
||
value = 3
|
||
```
|
||
|
||
**架构优势总结:**
|
||
|
||
| 特性 | 实现方式 |
|
||
|------|----------|
|
||
| 跨平台通知 | `notify-rust` 自动适配 Windows/macOS/Linux |
|
||
| 并发计时器 | `tokio::spawn` 每任务独立异步协程,互不阻塞 |
|
||
| 极低资源占用 | 全程 `async/await` sleep,不轮询 CPU |
|
||
| 配置驱动 | TOML + serde,零代码添加新任务 |
|
||
| 二进制极小 | release + LTO + strip,约 2-4MB | |