server.ts 2.8 KB

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