feat: 添加 /health 健康检查端点

- 新增 GET /health 端点,公开无需认证
- 检查 PostgreSQL 数据库连接状态
- 健康时返回: {"status": "healthy", "database": "connected"}
- 不健康时返回 HTTP 503 + {"status": "unhealthy", "database": "disconnected", "error": "..."}
- 失败时记录错误到 JSON 日志
- 用于负载均衡器、Kubernetes、1Panel 健康探针
- 更新 IMPROVEMENTS.md 标记 6.3 完成
This commit is contained in:
2026-04-15 10:49:15 +08:00
parent 741d6acff1
commit 338871796a
2 changed files with 24 additions and 2 deletions

View File

@@ -680,6 +680,26 @@ async fn save_user_profile(
}
}
// 健康检查端点
#[get("/health")]
async fn health_check(pool: web::Data<PgPool>) -> impl Responder {
// 检查数据库连接
match sqlx::query("SELECT 1").fetch_one(pool.get_ref()).await {
Ok(_) => HttpResponse::Ok().json(serde_json::json!({
"status": "healthy",
"database": "connected"
})),
Err(e) => {
tracing::error!("健康检查失败: {}", e);
HttpResponse::ServiceUnavailable().json(serde_json::json!({
"status": "unhealthy",
"database": "disconnected",
"error": e.to_string()
}))
}
}
}
fn create_server_config(
pool: PgPool,
http_client: Client,
@@ -698,6 +718,7 @@ fn create_server_config(
.app_data(web::Data::new(http_client))
.app_data(web::Data::new(app_state))
.service(web::resource("/static/{tail:.*}").route(web::get().to(serve_static_files)))
.service(health_check)
.service(login)
.service(get_weather_details)
.service(