server.ts 2.8 KB

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