server.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import express from 'express';
  2. import { readFileSync } from 'node:fs';
  3. const Parse = require('parse');
  4. Parse.initialize('hb-voc');
  5. Parse.serverURL = 'http://hb-server.fmode.cn/parse';
  6. Parse.masterKey = 'hb2026pallt';
  7. // @ts-ignore
  8. globalThis.Parse = Parse;
  9. // 获取配置
  10. function getConfig() {
  11. try {
  12. const configPath = process.env.NODE_ENV === 'test' ? './config.test.json' : './config.json';
  13. const configContent = readFileSync(configPath, 'utf-8');
  14. return JSON.parse(configContent);
  15. } catch (error: unknown) {
  16. const errorMessage = error instanceof Error ? error.message : 'Unknown error';
  17. console.warn('Config file not found, using defaults:', errorMessage);
  18. return {
  19. parse: {
  20. port: 10003,
  21. appId: 'hb-voc',
  22. serverURL: 'http://hb-server.fmode.cn/parse'
  23. }
  24. };
  25. }
  26. }
  27. // 初始化 Express 应用
  28. const app = express();
  29. // 中间件配置
  30. app.use(express.json());
  31. app.use(express.urlencoded({ extended: true }));
  32. // 日志中间件
  33. app.use((req: any, res: any, next: () => void) => {
  34. console.log(`[${new Date().toISOString()}] ${req.method} ${req.path}`);
  35. next();
  36. });
  37. // 健康检查端点
  38. app.get('/health', (req: any, res: any) => {
  39. res.json({
  40. status: 'healthy',
  41. timestamp: new Date().toISOString(),
  42. uptime: process.uptime()
  43. });
  44. });
  45. // 根路由
  46. app.get('/', (req: any, res: any) => {
  47. res.json({
  48. message: 'Fmode Server - Gongzuo API',
  49. version: '1.0.0',
  50. timestamp: new Date().toISOString(),
  51. endpoints: ['/api', '/health']
  52. });
  53. });
  54. // 404 处理
  55. app.use((req: any, res: any) => {
  56. res.status(404).json({
  57. message: 'Not Found',
  58. path: req.path,
  59. method: req.method
  60. });
  61. });
  62. // 错误处理中间件
  63. app.use((err: any, req: any, res: any, next: any) => {
  64. console.error('Error:', err);
  65. res.status(500).json({
  66. message: 'Internal Server Error',
  67. error: err.message
  68. });
  69. });
  70. // 启动服务器
  71. async function startServer() {
  72. const config = getConfig();
  73. const PORT = process.env.PORT || config.parse?.port || 3000;
  74. app.listen(PORT, () => {
  75. console.log('\n' + '='.repeat(60));
  76. console.log('🚀 Fmode Server - MSQ Started');
  77. console.log('='.repeat(60));
  78. console.log(`Timestamp: ${new Date().toISOString()}`);
  79. console.log('='.repeat(60) + '\n');
  80. });
  81. }
  82. // 优雅关闭
  83. process.on('SIGTERM', () => {
  84. console.log('\n📛 SIGTERM signal received: closing HTTP server');
  85. process.exit(0);
  86. });
  87. process.on('SIGINT', () => {
  88. console.log('\n📛 SIGINT signal received: closing HTTP server');
  89. process.exit(0);
  90. });
  91. // 启动服务器
  92. await startServer();