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:
@@ -201,7 +201,7 @@
|
||||
- 完成时间:2026-04-15
|
||||
- 修改文件:Cargo.toml, src/main.rs, src/config.rs
|
||||
|
||||
- [ ] **无日志聚合**
|
||||
- [x] **无日志聚合**
|
||||
- 需求:集中式日志管理
|
||||
- 实现:配置日志收集器(如 ELK Stack)
|
||||
- 格式:JSON 格式便于解析
|
||||
@@ -224,10 +224,11 @@
|
||||
- 工具:Prometheus + Grafana
|
||||
|
||||
### 6.3 健康检查
|
||||
- [ ] **缺少健康检查接口**
|
||||
- [x] **缺少健康检查接口** ✅
|
||||
- 需求:负载均衡器健康检查
|
||||
- 实现:添加 `/health` 端点
|
||||
- 检查:数据库连接、服务状态
|
||||
- 完成时间:2026-04-15
|
||||
|
||||
## 七、部署改进
|
||||
|
||||
|
||||
21
src/main.rs
21
src/main.rs
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user