-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathserver.js
More file actions
57 lines (42 loc) · 1.2 KB
/
server.js
File metadata and controls
57 lines (42 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env node
import 'dotenv/config.js'
import process from 'node:process'
import express from 'express'
import cors from 'cors'
import morgan from 'morgan'
import {createCluster} from 'addok-cluster'
import routes from './lib/routes.js'
const PORT = process.env.PORT || 5000
const app = express()
const cluster = await createCluster({
onTerminate(reason) {
console.log(`Cluster terminated: ${reason}`)
process.exit(0)
}
})
app.disable('x-powered-by')
if (process.env.NODE_ENV !== 'production') {
app.use(morgan('dev'))
}
app.use(cors({origin: true}))
app.use('/', routes(cluster))
const httpServer = app.listen(PORT, () => {
console.log(`Start listening on port ${PORT}`)
})
// Graceful shutdown on SIGTERM and SIGINT
async function handleShutdown(signal) {
console.log(`Received ${signal}, gracefully shutting down...`)
// Close HTTP server first
httpServer.close(() => {
console.log('Server closed')
})
// Then terminate the cluster
try {
await cluster.end()
} catch (error) {
console.error('Error during cluster shutdown:', error)
process.exit(1)
}
}
process.on('SIGTERM', () => handleShutdown('SIGTERM'))
process.on('SIGINT', () => handleShutdown('SIGINT'))