server.ts 3.3 KB

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