server.ts 3.0 KB

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