milky0217 2ce97243ab
Some checks failed
Deploy Backend / deploy (push) Has been cancelled
fix: 全量代码审计修复 — 30项
P0 - Panic 风险修复:
  - payment.rs: unwrap() → let-else safe handling
  - payment.rs: get_jwt_secret() expect → Result/AppError
  - auth.rs: openid 切片添加 len >= 8 守卫
  - main.rs: 启动时 expect → unwrap_or_else 描述性 panic
  - main.rs: Directive::from_str 添加 fallback

P1 - 逻辑/安全修复:
  - payment.rs: urlencoding() + 解码 bug 修复 (移除 had_escape)
  - payment.rs: Mock 支付添加 check_mock_payment_allowed 检查
  - db.rs: 永久会员 NULL → 2099-12-31 一致化
  - user.rs: 维护模式添加安全说明注释
  - 自动清理 unused_variables 警告 (_is_mobile)

P2 - 错误吞没修复:
  - main.rs: 3 处定时任务 let _ = → if let Err = tracing::error!
  - db.rs + admin.rs: 7 处通知/审计日志 let _ = → tracing::warn!
  - auth.rs: refresh token 保存 add warn 日志

P3 - 死代码清理:
  - models.rs: 移除 TokenResponse (dead)
  - models.rs: 移除 AppState 中 5 个未使用字段 (env var 直接读取)
  - error.rs: 移除 3 个 dead ErrorResponse 方法
  - rate_limiter.rs: extract_client_ip_from_header → #[cfg(test)]
  - models.rs: 注释 typo fix (user_ytpe → user_type)
  - db.rs: RefreshToken 添加 deserialization 注释

Shell 脚本修复:
  - deploy.sh: run_migrations 移到 restart_service 之前
  - test.sh: 移除 EXIT trap 覆盖; heredoc 引号修复; 维护模式添加 restart
  - common.sh: mock_key 添加 sed 转义 (防 / & 注入)

验证: cargo check 0 warnings, 8 tests passed
2026-07-23 12:40:47 +08:00
2026-05-26 15:20:50 +08:00

ASD Rust Backend

大气稳定度判定系统的后端服务,基于 Rust + actix-web 4。

快速开始

# 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 环境变量选择配置:

APP_ENV=development cargo run   # 本地开发
APP_ENV=production cargo run    # 生产环境

环境变量(.env

敏感信息仅通过 .env 文件加载,不出现在 Git 跟踪的配置文件中:

DATABASE_URL=postgres://user:pass@host:5432/dbname
WECHAT_APPID=wx...
WECHAT_SECRET=xxx
JWT_SECRET=openssl rand -base64 32 生成的随机密钥

部署

# 部署到开发服务器
./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

运维

# 数据库备份
./scripts/backup-db.sh

# 清理过期 refresh_token
./scripts/cleanup_refresh_tokens.sh

# 查看日志
journalctl -u rust-backend.service -n 100

开发规范

  • 所有提交使用 jjjujutsu v0.41+),详见 AGENTS.md
  • 支付宝沙箱测试使用 MOCK_LOGIN_ENABLED=true
  • 无支付宝配置时自动启用 Mock 支付模式
Description
ASD Rust Backend
Readme 2.7 MiB
Languages
JavaScript 77%
Rust 12.1%
Shell 6.9%
CSS 3.7%
HTML 0.2%
Other 0.1%