Upload server.js
Browse files- src/server.js +64 -3
src/server.js
CHANGED
|
@@ -3,11 +3,12 @@ const cors = require('cors')
|
|
| 3 |
const { SERVER_CONFIG } = require('./config')
|
| 4 |
const modelRouter = require('./router/model')
|
| 5 |
const chatRouter = require('./router/chat')
|
|
|
|
| 6 |
|
| 7 |
const app = express()
|
| 8 |
app.use(cors())
|
| 9 |
|
| 10 |
-
// 增加请求体积限制,设置为
|
| 11 |
app.use(express.json({ limit: '128mb' }))
|
| 12 |
app.use(express.urlencoded({ extended: true, limit: '128mb' }))
|
| 13 |
app.use(express.raw({ limit: '128mb' }))
|
|
@@ -16,7 +17,67 @@ app.use(express.raw({ limit: '128mb' }))
|
|
| 16 |
app.use('/v1/models', modelRouter)
|
| 17 |
app.use('/v1/chat', chatRouter)
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
// 启动服务器
|
| 20 |
-
app.listen(SERVER_CONFIG.PORT, () => {
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
})
|
|
|
|
|
|
|
|
|
| 3 |
const { SERVER_CONFIG } = require('./config')
|
| 4 |
const modelRouter = require('./router/model')
|
| 5 |
const chatRouter = require('./router/chat')
|
| 6 |
+
const logger = require('./lib/logger')
|
| 7 |
|
| 8 |
const app = express()
|
| 9 |
app.use(cors())
|
| 10 |
|
| 11 |
+
// 增加请求体积限制,设置为128MB
|
| 12 |
app.use(express.json({ limit: '128mb' }))
|
| 13 |
app.use(express.urlencoded({ extended: true, limit: '128mb' }))
|
| 14 |
app.use(express.raw({ limit: '128mb' }))
|
|
|
|
| 17 |
app.use('/v1/models', modelRouter)
|
| 18 |
app.use('/v1/chat', chatRouter)
|
| 19 |
|
| 20 |
+
// 健康检查端点
|
| 21 |
+
app.get('/health', (req, res) => {
|
| 22 |
+
res.json({
|
| 23 |
+
status: 'ok',
|
| 24 |
+
timestamp: new Date().toISOString(),
|
| 25 |
+
uptime: process.uptime()
|
| 26 |
+
})
|
| 27 |
+
})
|
| 28 |
+
|
| 29 |
+
// 全局错误处理中间件
|
| 30 |
+
app.use((error, req, res, next) => {
|
| 31 |
+
logger.logError('GLOBAL_ERROR', 'UNHANDLED_ERROR', error.message, {
|
| 32 |
+
stack: error.stack,
|
| 33 |
+
url: req.url,
|
| 34 |
+
method: req.method
|
| 35 |
+
})
|
| 36 |
+
|
| 37 |
+
res.status(500).json({
|
| 38 |
+
error: {
|
| 39 |
+
message: '服务器内部错误',
|
| 40 |
+
type: 'server_error'
|
| 41 |
+
}
|
| 42 |
+
})
|
| 43 |
+
})
|
| 44 |
+
|
| 45 |
+
// 全局异常捕获
|
| 46 |
+
process.on('uncaughtException', (error) => {
|
| 47 |
+
logger.logError('PROCESS_ERROR', 'UNCAUGHT_EXCEPTION', error.message, {
|
| 48 |
+
stack: error.stack
|
| 49 |
+
})
|
| 50 |
+
console.error('未捕获的异常:', error)
|
| 51 |
+
process.exit(1)
|
| 52 |
+
})
|
| 53 |
+
|
| 54 |
+
process.on('unhandledRejection', (reason, promise) => {
|
| 55 |
+
logger.logError('PROCESS_ERROR', 'UNHANDLED_REJECTION', String(reason), {
|
| 56 |
+
promise: promise.toString()
|
| 57 |
+
})
|
| 58 |
+
console.error('未处理的 Promise 拒绝:', reason)
|
| 59 |
+
})
|
| 60 |
+
|
| 61 |
+
// 优雅关闭处理
|
| 62 |
+
process.on('SIGTERM', () => {
|
| 63 |
+
console.log('收到 SIGTERM 信号,开始优雅关闭...')
|
| 64 |
+
process.exit(0)
|
| 65 |
+
})
|
| 66 |
+
|
| 67 |
+
process.on('SIGINT', () => {
|
| 68 |
+
console.log('收到 SIGINT 信号,开始优雅关闭...')
|
| 69 |
+
process.exit(0)
|
| 70 |
+
})
|
| 71 |
+
|
| 72 |
// 启动服务器
|
| 73 |
+
const server = app.listen(SERVER_CONFIG.PORT, () => {
|
| 74 |
+
logger.logInfo('SERVER_START', '服务器启动成功', {
|
| 75 |
+
port: SERVER_CONFIG.PORT,
|
| 76 |
+
url: `http://localhost:${SERVER_CONFIG.PORT}`,
|
| 77 |
+
nodeVersion: process.version,
|
| 78 |
+
platform: process.platform
|
| 79 |
+
})
|
| 80 |
+
console.log(`服务已启动在 http://localhost:${SERVER_CONFIG.PORT}`)
|
| 81 |
})
|
| 82 |
+
|
| 83 |
+
module.exports = { app, server }
|