File size: 2,674 Bytes
28e1dba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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;