-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
104 lines (84 loc) · 2.27 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
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
const app = require('express')();
const chalk = require('chalk');
const config = require('./dash.config.js');
const { fetchProc } = require('./modules/pm2.js');
const moment = require('moment');
let db;
(async () => {
try {
db = require('./modules/db/db.js')();
} catch (e) {}
await require('./modules/db/prepareDb')(db);
require('./modules/express/express.config.js')(app, db);
require('./modules/express/express.routes.js')(app, db);
})();
Array.prototype.random = function(count) {
if (!count) count = 1;
let items = this[Math.floor(Math.random() * this.length)];
if (count > 1) {
items = [];
for (let i = 0; i < count; i++) {
items.push(this[Math.floor(Math.random() * this.length)]);
}
}
return items;
};
Array.prototype.filter = function(condition) {
let arr = [];
this.forEach(item => {
if (condition(item) == true) arr.push(item);
});
return arr;
};
Array.prototype.getElements = function(element) {
let returnArr = [];
this.forEach(item => {
let el = item[element];
if (!returnArr.includes(el)) returnArr.push(el);
});
return returnArr;
};
Array.prototype.search = function(query) {
let arr = [];
this.forEach(item => {
if (query(item)) arr.push(item);
});
return arr;
};
let processes = [];
(async () => {
await startup();
})();
//Run express webserver
if (config.BIND_IP.enabled == true) {
app.listen(config.PORT, config.BIND_IP.ip, () => {
console.log(
chalk.green(`[WEBSERVER]: Server listening on port ${config.PORT} and bound to IP ${config.BIND_IP.ip}.`)
);
});
} else {
app.listen(config.PORT, () => {
console.log(chalk.green(`[WEBSERVER]: Server listening on port ${config.PORT}`));
});
}
async function startup() {
await fetchProc(processes);
console.log(chalk.blue('[PROCESSES]: Processes fetched successfully!', JSON.stringify(processes)));
await require('./modules/db/prepareDb')(db);
await refetchProcesses();
}
let fetchInterval;
async function refetchProcesses() {
// return;
fetchInterval = setInterval(async () => {
processes = [];
await fetchProc(processes);
console.log(chalk.blue('[PROCESSES]: Processes refetched successfully!', JSON.stringify(processes)));
db.push('/processes', processes);
}, 10 * 1000);
}
async function stopFetching() {
if (fetchInterval) {
clearTimeout(fetchInterval);
}
}