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*、构建/缓存产物。
This commit is contained in:
42
app/controller/health.js
Normal file
42
app/controller/health.js
Normal file
@@ -0,0 +1,42 @@
|
||||
'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;
|
||||
Reference in New Issue
Block a user