/
app.js
65 lines (58 loc) · 2.1 KB
/
app.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require("dotenv").config();
const express = require("express");
const mongoose = require("mongoose");
const app = express();
const dbConfig = require("./config/database-config.js");
const initializeDB = require("./app/scripts/initialize-db.js");
const validateCollections = require("./app/scripts/collections-validation.js");
const logger = require("./app/services/logger.js");
const syncCveScheduler = require("./app/services/sync-cve-scheduler.js");
const syncCpeScheduler = require("./app/services/sync-cpe-scheduler.js");
function logRequest(req, res, next) {
logger.info(req.url);
next();
}
function logError(err, req, res, next) {
logger.error(err);
next();
}
app.use(logRequest);
app.use(logError);
app.use(express.json());
require('./app/routes/cvedetails.routes.js')(app);
app.use('/', require('./app/routes/healthcheck.routes'));
const PORT = process.env.PORT || 3000;
app.listen(PORT, () =>
logger.info(`[SERVER] Server is running and listening on port ${PORT}.`)
);
mongoose.connect(dbConfig.url, dbConfig.options);
const database = mongoose.connection;
database.on("error", (error) => {
logger.error(
"[DATABASE] Could not connect to the database. Exiting now...",
error
);
process.exit();
});
database.once("connected", async () => {
logger.info("[DATABASE] Successfully connected to the database");
let isCollectionsExist = validateCollections(database)
if ((await isCollectionsExist).isExistCVE) {
logger.info("[VALIDATION] CVE details collection exists | Skipping import process");
}
else {
logger.info("[VALIDATION] CVE details collection doesn't exist | Starting import process");
await initializeDB.importCVE(database);
await initializeDB.firstCVEImport();
}
if ((await isCollectionsExist).isExistCPE) {
logger.info("[VALIDATION] CPE matches collection exists | Skipping import process");
}
else {
logger.info("[VALIDATION] CPE matches collection doesn't exist | Starting import process");
await initializeDB.importCPE(database, false);
await initializeDB.firstCPEImport();
}
syncCveScheduler.syncCVE();
syncCpeScheduler.syncCPE(database);
});