fix: harden launch server runtime and public config
This commit is contained in:
+39
-1
@@ -16,6 +16,7 @@ const routes = require('./routes')
|
||||
const PORT = Number(process.env.PORT) || 3600
|
||||
const HOST = process.env.HOST || '0.0.0.0'
|
||||
const IS_PRODUCTION = process.env.NODE_ENV === 'production'
|
||||
let server = null
|
||||
|
||||
// CORS: in production, require explicit allowlist; in dev, allow all with credentials
|
||||
function buildCorsOptions() {
|
||||
@@ -103,6 +104,7 @@ async function main() {
|
||||
|
||||
// Skip JSON body-parser for binary upload routes (busboy handles multipart parsing)
|
||||
app.use('/api/oss/upload-binary', (req, res, next) => { req._body = true; next(); })
|
||||
app.use("/api/files/extract-text", (req, res, next) => { req._body = true; next(); })
|
||||
// JSON body limit: 5MB globally (upload routes override locally)
|
||||
app.use('/api/oss/upload', express.json({ limit: '200mb' }))
|
||||
app.use(express.json({ limit: process.env.JSON_BODY_LIMIT || '5mb' }))
|
||||
@@ -145,7 +147,7 @@ async function main() {
|
||||
const { startStaleTaskCleanup } = require('./aiTaskWorker')
|
||||
startStaleTaskCleanup()
|
||||
|
||||
app.listen(PORT, HOST, () => {
|
||||
server = app.listen(PORT, HOST, () => {
|
||||
console.log(`OmniAI Key Server running at http://${HOST}:${PORT}`)
|
||||
console.log(`Health check: http://${HOST}:${PORT}/api/health`)
|
||||
})
|
||||
@@ -158,9 +160,45 @@ main().catch((err) => {
|
||||
|
||||
process.on('unhandledRejection', (reason) => {
|
||||
console.error('[fatal] Unhandled promise rejection:', reason)
|
||||
process.exitCode = 1
|
||||
setTimeout(() => process.exit(1), 5000).unref()
|
||||
})
|
||||
|
||||
process.on('uncaughtException', (err) => {
|
||||
console.error('[fatal] Uncaught exception:', err)
|
||||
process.exit(1)
|
||||
})
|
||||
|
||||
|
||||
// ── Graceful shutdown ───────────────────────────────────────────────────
|
||||
let shuttingDown = false
|
||||
|
||||
function gracefulShutdown(signal) {
|
||||
if (shuttingDown) return
|
||||
shuttingDown = true
|
||||
console.log('[shutdown] Received ' + signal + ', draining connections...')
|
||||
|
||||
if (server && server.listening) {
|
||||
server.close(() => {
|
||||
console.log('[shutdown] Server closed, cleaning up...')
|
||||
const { stopProviderHealthMonitor } = require('./providerHealthMonitor')
|
||||
stopProviderHealthMonitor()
|
||||
const { pool } = require('./db')
|
||||
pool.end().then(() => {
|
||||
console.log('[shutdown] Database pool closed')
|
||||
process.exit(0)
|
||||
}).catch(() => process.exit(0))
|
||||
})
|
||||
|
||||
// Force exit after timeout
|
||||
setTimeout(() => {
|
||||
console.error('[shutdown] Forced exit after timeout')
|
||||
process.exit(1)
|
||||
}, 15000).unref()
|
||||
} else {
|
||||
process.exit(0)
|
||||
}
|
||||
}
|
||||
|
||||
process.on('SIGTERM', () => gracefulShutdown('SIGTERM'))
|
||||
process.on('SIGINT', () => gracefulShutdown('SIGINT'))
|
||||
|
||||
Reference in New Issue
Block a user