feat: 实现后端多环境配置机制

- 使用 toml crate 替代 config crate 直接读取配置文件
- 创建 config/default.toml、config/development.toml、config/production.toml
- 重写 config.rs 支持多环境配置加载
- 增强 deploy.sh 支持 development/production 环境参数
- 更新 IMPROVEMENTS.md 标记环境区分完成
This commit is contained in:
2026-04-17 11:11:13 +08:00
parent ca7ddcc55e
commit da4e6f557e
8 changed files with 314 additions and 62 deletions

View File

@@ -22,7 +22,7 @@ src/
├── auth.rs # JWT 中间件,令牌生成/验证
├── db.rs # 数据库操作(通过 sqlx 执行原始 SQL
├── models.rs # 数据结构Claims、User、WeatherData 等)
├── config.rs # 配置结构体(未使用 — 死代码,待清理
├── config.rs # 配置加载支持多环境config/*.toml
└── handlers/ # 路由处理器模块
├── mod.rs # 模块导出
├── auth.rs # 登录相关 (login)
@@ -32,6 +32,11 @@ src/
├── health.rs # 健康检查 (/health)
└── static_files.rs # 静态文件服务 (/static/{path})
config/
├── default.toml # 默认配置(所有环境的共同默认值)
├── development.toml # 开发/测试环境配置
└── production.toml # 生产环境配置
migrations/
└── 001_add_payment_fields.sql # 用于添加支付字段的 ALTER TABLE
@@ -40,6 +45,9 @@ static/
tests/
└── integration_test.rs # 基础测试框架
deploy.sh # 部署脚本(支持 development/production 参数)
.env.example # 环境变量模板
```
## 数据库表结构
@@ -149,18 +157,54 @@ pub async fn function_name(pool: &PgPool, param: i32) -> Result<Type, String> {
## 环境变量
```env
DATABASE_URL=postgres://user:pass@host:5432/dbname
WECHAT_APPID=wx...
WECHAT_SECRET=...
JWT_SECRET=your_secret_key
SSL_KEY_PATH=/path/to/key
SSL_CERT_PATH=/path/to/cert
RUST_LOG=info
APP_VERSION=0.2.0
FREE_USER_DATA_LIMIT=20
### 方式一:使用配置文件(推荐)
通过 `APP_ENV` 环境变量选择配置文件:
```bash
APP_ENV=development cargo run # 使用 config/development.toml
APP_ENV=production cargo run # 使用 config/production.toml
```
**配置文件**
| 文件 | 用途 |
|------|------|
| `config/default.toml` | 所有环境的共同默认值 |
| `config/development.toml` | 开发/测试环境覆盖 |
| `config/production.toml` | 生产环境覆盖 |
**示例 production.toml**
```toml
database_url = "postgres://user:pass@host:5432/dbname"
wechat_appid = "wx..."
jwt_secret = "your_secret_key"
rust_log = "info"
free_user_data_limit = 20
server_host = "0.0.0.0"
server_ports = [4433, 8443, 8080, 3000, 8000, 8888]
```
### 方式二:直接环境变量
最高优先级,可覆盖配置文件:
```env
APP_DATABASE_URL=postgres://user:pass@host:5432/dbname
APP_JWT_SECRET=your_secret_key
APP_WECHAT_APPID=wx...
APP_RUST_LOG=info
```
### systemd service 配置
测试服务和生产服务通过不同 systemd unit 和工作目录隔离:
| 服务 | systemd unit | 工作目录 | APP_ENV |
|------|-------------|---------|---------|
| 测试 | `rust-backend-dev.service` | `/root/rust/rust_backend_dev` | `development` |
| 生产 | `rust-backend.service` | `/root/rust/rust_backend` | (使用 .env) |
## 约束条件
### 已知兼容性问题
@@ -178,7 +222,6 @@ FREE_USER_DATA_LIMIT=20
- 在 JWT Claims 中添加 `is_admin`(必须查询数据库)
- 信任 JWT 中的 `is_paid` 来做配额决策(必须查询数据库)
- 使用 `as any``@ts-ignore` 或类型错误抑制
- 修改 config.rs死代码
- 添加支付网关集成
- 添加审计日志
@@ -232,10 +275,17 @@ App::new()
## 构建与运行
```bash
cargo build # 编译
cargo run # 启动服务器
cargo test # 运行测试
cargo clippy # 代码检查
cargo build # 编译
APP_ENV=development cargo run # 开发环境运行
APP_ENV=production cargo run # 生产环境运行
cargo test # 运行测试
cargo clippy # 代码检查
```
**部署**
```bash
./deploy.sh development # 部署到测试服务器
./deploy.sh production # 部署到生产服务器
```
服务器尝试端口顺序4433、8443、8080、3000、8000、8888