-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
59 lines (50 loc) · 1.51 KB
/
index.ts
File metadata and controls
59 lines (50 loc) · 1.51 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
58
59
import { Express } from 'express'
import { Server } from 'http'
import { app, httpServer } from './app'
import mongoose from 'mongoose'
// const app = require('./app')
// const config = require('./config/config')
import { logger } from './config/logger'
import { AppDataSource } from './models/typeorm/data-source'
import dotenv from 'dotenv'
dotenv.config()
let server: Server
const mongodb_url = `mongodb+srv://${process.env.MONGO_USERNAME}:${process.env.MONGO_PASSWORD}@${process.env.MONGO_LOCATION}/?retryWrites=true&w=majority`
export const mongoose_connect = mongoose.connect(mongodb_url)
const typeorm_connect = AppDataSource.initialize()
const promises = []
promises.push(mongoose_connect)
promises.push(typeorm_connect)
Promise.all(promises)
.then(() => {
logger.info('Connected to MongoDB & MySQL')
server = httpServer.listen(process.env.APP_PORT, () => {
logger.info(`Listening to port ${process.env.APP_PORT}`)
})
})
.catch((e) => {
logger.error('MongoDB or MySQL Connection Failed')
logger.error(e)
})
const exitHandler = () => {
if (server) {
server.close(() => {
logger.info('Server closed')
process.exit(1)
})
} else {
process.exit(1)
}
}
const unexpectedErrorHandler = (error: any) => {
logger.error(error.stack)
exitHandler()
}
process.on('uncaughtException', unexpectedErrorHandler)
process.on('unhandledRejection', unexpectedErrorHandler)
process.on('SIGTERM', () => {
logger.info('SIGTERM received')
if (server) {
server.close()
}
})