-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
75 lines (70 loc) · 2.25 KB
/
server.ts
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import * as dotenv from 'dotenv';
const env = dotenv.config();
if (env.error) {
dotenv.config({ path: '.env' });
}
import ContractEventService from './src/api/v1/services/contractEvents.service';
import MongoConnection from './src/api/v1/config/db.config';
import app from './src/api/v1/app';
const host = process.env.HOST || 'localhost';
const port = process.env.PORT || '62562';
let dbConn: MongoConnection;
app.listen(port, () => {
console.log({
level: 'info',
message: `🌏 Express server started on http://${host}:${port}`,
});
// Establish the Contract Services
new ContractEventService();
// Establish the DB connection
dbConn = new MongoConnection(
process.env.MONGO_URI ? process.env.MONGO_URI : '', // FIXME: Add fallback URI
);
dbConn.connect(() => {});
}).on('error', (err: any): void => {
if (err.code === 'EADDRINUSE') {
console.log({
level: 'error',
message: 'Server startup error: address already in use',
error: err,
});
} else {
console.log(err);
}
});
// Close the Mongoose connection, when receiving SIGINT
process.on('SIGINT', (): void => {
console.info('\nGracefully shutting down');
dbConn.close((err: any) => {
if (err) {
console.log({
level: 'error',
message: 'Error shutting closing mongo connection',
error: err,
});
}
process.exit(0);
});
});
// // Establish the image storage engine
// const storage = new GridFsStorage({
// url: process.env.MONGO_URI ? process.env.MONGO_URI : '',
// file: (req, file) => {
// return new Promise((resolve, reject) => {
// randomBytes(16, (err, buf) => {
// if (err) {
// return reject(err);
// }
// const filename = `${buf.toString('hex')}${path.extname(
// file.originalname,
// )}`;
// const fileinfo = {
// filename: filename,
// bucketName: 'imageUploads',
// };
// resolve(fileinfo);
// });
// });
// },
// });
// const upload = multer({ storage });