Files
competition-website/app/controller/health.js
WorkBuddy a303cf2d53 chore: 后端首次基线提交
运行必须源码:app/(controller/model/service/middleware/lib/utils/validate/router)、config/、database/(migrations/seeders/sql)、启动脚本、app/public/templates 模板。
已忽略运行非必须:node_modules/、logs/、run/、app/public 运行时上传数据、.env*、构建/缓存产物。
2026-08-17 12:25:42 +08:00

43 lines
1.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict';
const Controller = require('egg').Controller;
/**
* 健康检查控制器 - HealthController
*
* 提供容器探针 / 负载均衡探活使用的轻量级接口。
* 接口:GET /api/health(公开,无需认证)
* 返回统一信封 { code: 0, data: { status, db, timestamp, uptime } }。
*
* 设计说明:
* - 进程存活即返回 HTTP 200;
* - db 字段反映数据库连通性('ok' / 'down'),便于运维监控但不会导致探针因瞬时抖动而误重启。
*/
class HealthController extends Controller {
async check() {
const { ctx, app } = this;
let db = 'ok';
try {
// egg-sequelize 将 Sequelize 实例直接挂载在 app.model 上(即 delegate)
await app.model.authenticate();
} catch (e) {
db = 'down';
ctx.logger.warn('[健康检查] 数据库连通性异常:', e.message);
}
ctx.status = 200;
ctx.body = ctx.helper.success(
{
status: 'ok',
db,
timestamp: Date.now(),
uptime: Math.floor(process.uptime()),
},
'ok'
);
}
}
module.exports = HealthController;