-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (37 loc) · 1.13 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
const mongoose = require('mongoose')
const { uri } = require('./utils/db_uri')
const { port, host } = require('./config/config')
const app = require('./server')
const { updateCoins } = require('./src/services/crypto_update.service')
const logger = require('./utils/logger')
let server
mongoose.connect(uri()).then(() => {
logger.info('Connected to MongoDB at', uri())
server = app.listen(port, host, () => {
logger.info(`Server is running on http://${host}:${port}`)
updateCoins() // First time sync
})
})
const db = mongoose.connection
db.on('error', (err) => {
logger.error('Failed to connect to MongoDB with error:', err)
process.exit(1)
})
process.on('uncaughtException', (error) => {
logger.error('Uncaught Exception:', error)
process.exit(1)
})
process.on('unhandledRejection', (reason, promise) => {
logger.error('Unhandled Rejection:', reason)
process.exit(1)
})
process.on('SIGINT', () => {
if (server) {
server.close()
logger.info('Server closed')
}
db.close().then(() => {
logger.info('MongoDB connection closed')
process.exit(0)
})
})