'use strict'; const { Controller } = require('egg'); class HealthController extends Controller { /** * GET /health * 同时探测 MySQL 与 Redis,任一不可用则返回 503 */ async index() { const { ctx, app } = this; const [ mysql, redis ] = await Promise.all([ ctx.service.health.checkMysql(), ctx.service.health.checkRedis(), ]); const ok = mysql.ok && redis.ok; ctx.status = ok ? 200 : 503; ctx.body = { ok, env: app.config.env, uptime: Math.round(process.uptime()), dependencies: { mysql, redis }, }; } } module.exports = HealthController;