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

@@ -8,11 +8,13 @@ use sqlx::postgres::PgPool;
use std::pin::Pin;
mod auth;
mod config;
mod db;
mod handlers;
mod models;
use auth::jwt_middleware;
use config::AppConfig;
use db::create_pool;
use handlers::{
admin_get_user, admin_update_user_payment, delete_weather, generate_temp_token_handler,
@@ -79,8 +81,25 @@ fn create_server_config(
#[actix_web::main]
async fn main() -> std::io::Result<()> {
if let Err(e) = dotenvy::dotenv() {
tracing::warn!("加载.env文件失败使用系统环境变量: {}", e);
// 加载配置文件(支持多环境)
let app_config = match AppConfig::load() {
Ok(cfg) => cfg,
Err(e) => {
eprintln!("配置加载失败: {}", e);
std::process::exit(1);
}
};
// 设置环境变量(向后兼容依赖 env var 的组件)
unsafe {
std::env::set_var("DATABASE_URL", &app_config.database_url);
std::env::set_var("JWT_SECRET", &app_config.jwt_secret);
std::env::set_var("WECHAT_APPID", &app_config.wechat_appid);
std::env::set_var("WECHAT_SECRET", &app_config.wechat_secret);
std::env::set_var("SSL_KEY_PATH", &app_config.ssl_key_path);
std::env::set_var("SSL_CERT_PATH", &app_config.ssl_cert_path);
std::env::set_var("RUST_LOG", &app_config.rust_log);
std::env::set_var("FREE_USER_DATA_LIMIT", app_config.free_user_data_limit.to_string());
}
// 初始化文件日志JSON 格式,带轮转)
@@ -102,9 +121,9 @@ async fn main() -> std::io::Result<()> {
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::from_default_env()
.add_directive("rust_backend=info".parse().unwrap())
.add_directive("actix_web=info".parse().unwrap())
.add_directive("sqlx=warn".parse().unwrap()) // SQLx 日志太多,降级
.add_directive(format!("rust_backend={}", app_config.rust_log).parse().unwrap())
.add_directive(format!("actix_web={}", app_config.rust_log).parse().unwrap())
.add_directive("sqlx=warn".parse().unwrap())
)
.with_target(true)
.with_thread_ids(false) // 生产环境可开启