Files
asd-backend/README.md
moira 27c8979690 fix: 全量代码审计修复—安全/死代码/错误处理/配置净化
P0 安全修复:
  - 支付宝回调验签 (alipay_notify BTreeMap + rsa2_verify)
  - JWT fallback 'default_secret' 改为 .expect() (panic保底)
  - 安全响应头中间件 (CSP/X-Frame-Options/HSTS)

P1 死代码清理:
  - 移除孤儿文件 src/alipay.rs (284行, 无mod注册)
  - 移除Cargo未使用依赖 (actix-files/error/log/hex/digest)
  - log::info! → tracing::info! (auth.rs)
  - 移除 config.rs server_host + 3个TOML定义

P1 质量修复:
  - 修复 weather.rs unwrap() → unwrap_or
  - 修复 auth.rs+payment.rs 错误吞咽 (add tracing::warn)
  - 修复 main.rs 3x parse().unwrap → unwrap_or

P3 运维:
  - 新增 scripts/backup-db.sh (定时备份用)
  - 新增 README.md (快速入门文档)
  - deploy.sh 集成 backup-db.sh 上传
2026-05-13 17:20:31 +08:00

128 lines
3.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# ASD Rust Backend
大气稳定度判定系统的后端服务,基于 Rust + actix-web 4。
## 快速开始
```bash
# 1. 复制环境配置模板
cp .env.example .env
# 编辑 .env 填入实际值(数据库连接、微信凭证等)
# 2. 运行开发服务器
APP_ENV=development cargo run
# 3. 运行测试
cargo test
```
## 技术栈
| 技术 | 用途 |
|------|------|
| Rust (edition 2024) | 主力语言 |
| actix-web 4.11 | Web 框架 |
| sqlx 0.8.6 | PostgreSQL 数据库驱动 |
| jsonwebtoken 9.3 | JWT 认证 |
| reqwest 0.12 | HTTP 客户端 |
| chrono 0.4 | 时间处理 |
## 项目结构
```
src/
├── main.rs # 入口、服务器配置、路由注册
├── auth.rs # JWT 中间件、令牌生成/验证
├── db.rs # 数据库操作
├── models.rs # 数据结构
├── config.rs # 多环境配置加载
├── error.rs # 错误处理
├── rate_limiter.rs # 滑动窗口 Rate Limiter
└── handlers/ # 路由处理器
├── mod.rs
├── meta.rs # 服务状态页
├── auth.rs # 登录/注册
├── weather.rs # 天气数据 CRUD
├── user.rs # 用户信息
├── admin.rs # 管理员功能
├── payment.rs # 支付(支付宝/Mock
├── favorites.rs # 收藏
├── health.rs # 健康检查
└── static_files.rs # 静态文件服务
config/ # 环境配置文件
migrations/ # 数据库迁移
scripts/ # 运维脚本
tests/ # 集成测试
```
## 环境管理
### 配置文件
通过 `APP_ENV` 环境变量选择配置:
```bash
APP_ENV=development cargo run # 本地开发
APP_ENV=production cargo run # 生产环境
```
### 环境变量(.env
**敏感信息仅通过 `.env` 文件加载**,不出现在 Git 跟踪的配置文件中:
```env
DATABASE_URL=postgres://user:pass@host:5432/dbname
WECHAT_APPID=wx...
WECHAT_SECRET=xxx
JWT_SECRET=openssl rand -base64 32 生成的随机密钥
```
## 部署
```bash
# 部署到开发服务器
./deploy.sh development
# 部署到生产服务器
./deploy.sh production
# 初始化服务器 .env生成随机 JWT_SECRET
./deploy.sh production --init-env
# 更多选项
./deploy.sh --help
```
## 关键 API
| 端点 | 说明 |
|------|------|
| `GET /health` | 健康检查 |
| `POST /api/login` | 微信登录 |
| `POST /api/post-weather-data` | 上传天气数据 |
| `GET /weather` | 分页查询天气数据 |
| `GET /payment` | 套餐选择页 |
| `POST /api/payment/sync-order` | 同步会员状态 |
完整 API 文档见 [AGENTS.md](./AGENTS.md)。
## 运维
```bash
# 数据库备份
./scripts/backup-db.sh
# 清理过期 refresh_token
./scripts/cleanup_refresh_tokens.sh
# 查看日志
journalctl -u rust-backend.service -n 100
```
## 开发规范
- 所有提交使用 `jj`jujutsu v0.41+),详见 AGENTS.md
- 支付宝沙箱测试使用 `MOCK_LOGIN_ENABLED=true`
- 无支付宝配置时自动启用 Mock 支付模式