-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (36 loc) · 1 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
const logger = require("./config/logger.js");
const mongooseSetup = require("./db/index.js");
const app = require("./app.js");
const contactRoute = require("./routes/contactRoute.js");
const redisStart = require("./db/redis.js");
const { PORT } = require("./config");
let attempt = 0;
const MAX_ATTEMPT = 5;
const runAPI = async () => {
attempt += 1;
logger.info("Try to run API");
try {
await mongooseSetup();
logger.info("MongoDB connected!!");
const redisClient = await redisStart();
logger.info("Redis connected!!");
app.use(function (req, res, next) {
req.redis = redisClient;
next();
});
app.get("/ping", (req, res) => {
res.send("PONG");
});
app.use("/api/contact", contactRoute);
return app;
} catch (err) {
logger.error(`${err} mongoDB didn't connect!`);
if (attempt < MAX_ATTEMPT) {
setTimeout(runApi, 10000);
} else {
logger.error("Exiting the process!");
process.exit(1);
}
}
};
module.exports = runAPI;