-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (61 loc) · 1.93 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
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
require('dotenv').config();
const express = require('express');
const db = require('./database/connection.js');
const { api, namespace, namespaceCheck, deployment, endpoints, pod, service, ingress, secret, configMap, certificatesigningrequest, clusterRole, role, clusterRoleBinding, roleBinding, openapi } = require('./routes/index.js');
const { killContainer, removeContainer } = require('./functions.js');
const { Object, Status } = require('./objects');
const nodeCleanup = require('node-cleanup');
let dnsServerIndex = process.argv.indexOf('-dnsServer');
if (dnsServerIndex === -1) {
console.error('Could not find local DNS server!');
console.error('Did you run npm start?');
process.exit(1);
}
let dbNameIndex = process.argv.indexOf('-dbName');
if (dbNameIndex !== -1) {
process.env.DB_URL = `mongodb://localhost:27017`;
}
process.env.DNS_SERVER = process.argv[dnsServerIndex + 1];
db.connect(process.env.DB_URL);
const app = express();
app.use(express.json());
app.use((req, res, next) => {
console.log(req.headers, req.body, req.url);
console.log('------------------');
next();
})
app.use(api);
app.use(openapi);
app.use(namespace);
app.use(namespaceCheck);
app.use(pod);
app.use(service);
app.use(ingress);
app.use(endpoints);
app.use(deployment);
app.use(secret);
app.use(configMap);
app.use((req, res) => {
res.status(404).send(Object.notFoundStatus());
})
app.use((err, req, res, next) => {
if (err instanceof Status) {
res.status(err.code).send(err);
} else {
console.error(err.stack);
console.log(err);
res.status(500).send(Object.internalServerErrorStatus());
}
return next();
});
app.listen(8080);
nodeCleanup(async (exitCode, signal) => {
if (signal) {
if (dbNameIndex != -1) {
console.log(process.argv[dbNameIndex + 1]);
await killContainer(process.argv[dbNameIndex + 1]);
await removeContainer(process.argv[dbNameIndex + 1]);
}
process.kill(process.pid, signal);
}
});