| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- import express from 'express';
- import { readFileSync } from 'node:fs';
- const Parse = require('parse');
- Parse.initialize('hb-voc');
- Parse.serverURL = 'http://hb-server.fmode.cn/parse';
- Parse.masterKey = 'hb2026pallt';
- // @ts-ignore
- globalThis.Parse = Parse;
- // 获取配置
- function getConfig() {
- try {
- const configPath = process.env.NODE_ENV === 'test' ? './config.test.json' : './config.json';
- const configContent = readFileSync(configPath, 'utf-8');
- return JSON.parse(configContent);
- } catch (error: unknown) {
- const errorMessage = error instanceof Error ? error.message : 'Unknown error';
- console.warn('Config file not found, using defaults:', errorMessage);
- return {
- parse: {
- port: 10003,
- appId: 'hb-voc',
- serverURL: 'http://hb-server.fmode.cn/parse'
- }
- };
- }
- }
- // 初始化 Express 应用
- const app = express();
- // 中间件配置
- app.use(express.json());
- app.use(express.urlencoded({ extended: true }));
- // 日志中间件
- app.use((req: any, res: any, next: () => void) => {
- console.log(`[${new Date().toISOString()}] ${req.method} ${req.path}`);
- next();
- });
- // 健康检查端点
- app.get('/health', (req: any, res: any) => {
- res.json({
- status: 'healthy',
- timestamp: new Date().toISOString(),
- uptime: process.uptime()
- });
- });
- // 根路由
- app.get('/', (req: any, res: any) => {
- res.json({
- message: 'Fmode Server - Gongzuo API',
- version: '1.0.0',
- timestamp: new Date().toISOString(),
- endpoints: ['/api', '/health']
- });
- });
- // 404 处理
- app.use((req: any, res: any) => {
- res.status(404).json({
- message: 'Not Found',
- path: req.path,
- method: req.method
- });
- });
- // 错误处理中间件
- app.use((err: any, req: any, res: any, next: any) => {
- console.error('Error:', err);
- res.status(500).json({
- message: 'Internal Server Error',
- error: err.message
- });
- });
- // 启动服务器
- async function startServer() {
- const config = getConfig();
- const PORT = process.env.PORT || config.parse?.port || 3000;
- app.listen(PORT, () => {
- console.log('\n' + '='.repeat(60));
- console.log('🚀 Fmode Server - MSQ Started');
- console.log('='.repeat(60));
- console.log(`Timestamp: ${new Date().toISOString()}`);
- console.log('='.repeat(60) + '\n');
- });
- }
- // 优雅关闭
- process.on('SIGTERM', () => {
- console.log('\n📛 SIGTERM signal received: closing HTTP server');
- process.exit(0);
- });
- process.on('SIGINT', () => {
- console.log('\n📛 SIGINT signal received: closing HTTP server');
- process.exit(0);
- });
- // 启动服务器
- await startServer();
|