import express from 'express'; import { performance } from 'perf_hooks'; import os from 'os'; const router = express.Router(); // 检查数据库连接状态 const checkDatabase = async () => { try { // 这里可以添加实际的数据库连接检查 return { status: 'healthy', message: 'Database connection OK' }; } catch (error) { return { status: 'unhealthy', message: error.message }; } }; // 检查浏览器服务状态 const checkBrowserServices = async () => { try { // 这里可以添加浏览器服务的健康检查 return { status: 'healthy', message: 'Browser services OK' }; } catch (error) { return { status: 'unhealthy', message: error.message }; } }; // 检查存储服务状态 const checkStorage = async () => { try { // 这里可以添加存储服务的健康检查 return { status: 'healthy', message: 'Storage services OK' }; } catch (error) { return { status: 'unhealthy', message: error.message }; } }; // 健康检查端点 router.get('/health', async (req, res) => { const startTime = performance.now(); try { const [database, browser, storage] = await Promise.all([ checkDatabase(), checkBrowserServices(), checkStorage() ]); const responseTime = performance.now() - startTime; const healthCheck = { status: 'OK', timestamp: new Date().toISOString(), uptime: process.uptime(), responseTime: `${responseTime.toFixed(2)}ms`, version: process.env.npm_package_version || '1.0.0', environment: process.env.NODE_ENV || 'development', memory: { used: Math.round(process.memoryUsage().heapUsed / 1024 / 1024 * 100) / 100, total: Math.round(process.memoryUsage().heapTotal / 1024 / 1024 * 100) / 100, system: Math.round(os.totalmem() / 1024 / 1024 * 100) / 100 }, services: { database, browser, storage } }; // 检查是否有任何服务不健康 const hasUnhealthyService = Object.values(healthCheck.services) .some(service => service.status === 'unhealthy'); const statusCode = hasUnhealthyService ? 503 : 200; res.status(statusCode).json(healthCheck); } catch (error) { const responseTime = performance.now() - startTime; res.status(503).json({ status: 'ERROR', timestamp: new Date().toISOString(), responseTime: `${responseTime.toFixed(2)}ms`, error: error.message }); } }); // 简单的存活检查 router.get('/ping', (req, res) => { res.status(200).json({ message: 'pong', timestamp: new Date().toISOString() }); }); export default router;