forked from kings-joy/12x3
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
50 lines (44 loc) · 1.31 KB
/
index.js
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
const express = require("express")
const { GracefulShutdownServer } = require("medusa-core-utils")
const loaders = require("@medusajs/medusa/dist/loaders/index").default
;(async() => {
async function start() {
const app = express()
const directory = process.cwd()
try {
const { container } = await loaders({
directory,
expressApp: app
})
const configModule = container.resolve("configModule")
const port = process.env.PORT ?? configModule.projectConfig.port ?? 9000
const server = GracefulShutdownServer.create(
app.listen(port, (err) => {
if (err) {
return
}
console.log(`Server is ready on port: ${port}`)
})
)
// Handle graceful shutdown
const gracefulShutDown = () => {
server
.shutdown()
.then(() => {
console.info("Gracefully stopping the server.")
process.exit(0)
})
.catch((e) => {
console.error("Error received when shutting down the server.", e)
process.exit(1)
})
}
process.on("SIGTERM", gracefulShutDown)
process.on("SIGINT", gracefulShutDown)
} catch (err) {
console.error("Error starting server", err)
process.exit(1)
}
}
await start()
})()