57 lines
1.5 KiB
JavaScript
57 lines
1.5 KiB
JavaScript
require('dotenv').config();
|
|
const createApp = require('./app');
|
|
|
|
/**
|
|
* 启动服务器
|
|
*/
|
|
function startServer() {
|
|
const app = createApp();
|
|
const PORT = process.env.PORT || 3000;
|
|
|
|
const server = app.listen(PORT, () => {
|
|
console.log('='.repeat(60));
|
|
console.log('🚀 Steel Prices Service 已启动');
|
|
console.log('='.repeat(60));
|
|
console.log(`📍 服务地址: http://localhost:${PORT}`);
|
|
console.log(`📍 Swagger文档地址: http://localhost:${PORT}/api-docs`);
|
|
console.log(`🌍 环境: ${process.env.NODE_ENV || 'development'}`);
|
|
console.log(`📊 API 文档: http://localhost:${PORT}/`);
|
|
console.log('='.repeat(60));
|
|
});
|
|
|
|
// 优雅关闭
|
|
const gracefulShutdown = (signal) => {
|
|
console.log(`\n${signal} 信号收到,正在关闭服务器...`);
|
|
server.close(() => {
|
|
console.log('✅ 服务器已关闭');
|
|
process.exit(0);
|
|
});
|
|
|
|
// 10秒后强制关闭
|
|
setTimeout(() => {
|
|
console.error('❌ 强制关闭服务器');
|
|
process.exit(1);
|
|
}, 10000);
|
|
};
|
|
|
|
process.on('SIGTERM', () => gracefulShutdown('SIGTERM'));
|
|
process.on('SIGINT', () => gracefulShutdown('SIGINT'));
|
|
|
|
// 未捕获的异常
|
|
process.on('unhandledRejection', (reason, promise) => {
|
|
console.error('❌ 未处理的 Promise 拒绝:', reason);
|
|
});
|
|
|
|
process.on('uncaughtException', (error) => {
|
|
console.error('❌ 未捕获的异常:', error);
|
|
process.exit(1);
|
|
});
|
|
}
|
|
|
|
// 如果直接运行此文件
|
|
if (require.main === module) {
|
|
startServer();
|
|
}
|
|
|
|
module.exports = startServer;
|