-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
126 lines (112 loc) · 3.71 KB
/
Copy pathmain.js
File metadata and controls
126 lines (112 loc) · 3.71 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import express from 'express';
import cors from 'cors';
import dotenv from 'dotenv';
import path from 'path';
import { fileURLToPath } from 'url';
import logger from './logger.js';
import { connectDB, disconnectDB } from './src/db.js';
import { connectRedis, disconnectRedis } from './src/cache.js';
import {
startKafkaConsumer,
consumer,
producer,
} from './src/services/kafkaConsumer.js';
import {
startModerationCron,
stopModerationCron,
} from './src/utils/moderationCron.js';
// import your middlewares here
import verifyToken from './src/middlewares/authMiddlewares.js';
// import your routes here
import aboutRoutes from './src/routes/aboutRoutes.js';
import healthRoutes from './src/routes/healthRoutes.js';
import commentRoutes from './src/routes/commentRoutes.js';
import ratingRoutes from './src/routes/ratingRoutes.js';
import playlistRoutes from './src/routes/playlistRoutes.js';
import moderationReportRoutes from './src/routes/moderationReportRoutes.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
dotenv.config({ path: path.resolve(__dirname, '.env'), quiet: true });
const PORT = process.env.PORT || 3000;
const app = express();
app.use(express.json());
app.use(cors());
// add your middlewares here like this:
app.use(verifyToken);
// add your routes here like this:
aboutRoutes(app);
healthRoutes(app);
commentRoutes(app);
ratingRoutes(app);
playlistRoutes(app);
moderationReportRoutes(app);
// Export app for tests. Do not remove this line
export default app;
let server;
if (process.env.NODE_ENV !== 'test') {
await connectDB();
if (process.env.ENABLE_REDIS.toLocaleLowerCase() === 'true') {
await connectRedis();
startModerationCron();
} else {
logger.warn('Redis is not enabled');
}
if (process.env.ENABLE_KAFKA.toLocaleLowerCase() === 'true') {
logger.warn('Kafka is enabled, trying to connect');
await startKafkaConsumer();
} else {
logger.warn('Kafka is not enabled');
}
server = app.listen(PORT, () => {
logger.warn(`Using log level: ${process.env.LOG_LEVEL}`);
logger.info(`API running at http://localhost:${PORT}`);
logger.info(`Health at http://localhost:${PORT}/api/v1/health`);
logger.info(`API docs running at http://localhost:${PORT}/api/v1/docs/`);
logger.info(`Environment: ${process.env.NODE_ENV}`);
});
}
async function gracefulShutdown(signal) {
logger.warn(`${signal} received. Starting secure shutdown...`);
if (process.env.ENABLE_REDIS.toLocaleLowerCase() === 'true') {
stopModerationCron();
}
try {
logger.warn('Disconnecting Kafka consumer...');
await consumer.disconnect();
logger.warn('Kafka consumer disconnected.');
logger.warn('Disconnecting Kafka producer...');
await producer.disconnect();
logger.warn('Kafka producer disconnected.');
} catch (err) {
logger.error('Error disconnecting Kafka:', err);
}
if (server) {
server.close(async () => {
logger.info('Server closed');
logger.info(
'Since now new connections are not allowed. Waiting for current operations to finish...'
);
try {
await disconnectRedis();
} catch (err) {
logger.error('Error disconnecting Redis:', err);
}
try {
await disconnectDB();
logger.info('MongoDB disconnected');
} catch (err) {
logger.error('Error disconnecting MongoDB:', err);
}
logger.info('Shutdown complete. Bye! ;)');
process.exit(0);
});
setTimeout(() => {
logger.error('Forced shutdown after timeout');
process.exit(1);
}, 30000);
} else {
process.exit(0);
}
}
process.on('SIGINT', () => gracefulShutdown('SIGINT'));
process.on('SIGTERM', () => gracefulShutdown('SIGTERM'));