-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
108 lines (98 loc) · 2.58 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
105
106
107
108
const express = require('express');
const bodyparser = require("body-parser");
const session = require("express-session");
const path = require("path");
const ejs = require("ejs");
const url = require("url");
const rateLimit = require("express-rate-limit")
const moment = require('moment')
var momento = require('moment-timezone');
const axios = require("axios")
const request = require('request')
const app = express();
var clc = require("cli-color");
const config = require('./config.json')
const now = moment().format("YYYY, MM, DD, HH:mm")
const port = 8080
const {
JsonDatabase
} = require("wio.db");
const db = new JsonDatabase({
databasePath: "./db/database.json"
});
const log = new JsonDatabase({
databasePath: "./db/logs.json"
});
// View engines & others
app.use(bodyparser.json());
app.use(bodyparser.urlencoded({ extended: true }));
app.engine("html", ejs.renderFile);
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, "/web/views"));
app.use(express.static(path.join(__dirname, "/web/public")));
app.set('json spaces', 1)
// Pages
app.get('/', (req, res) => {
const vsl = Object.values(config.links)
res.render('index', {
user: req.user,
config, req, vsl, db, moment, log
})
})
setInterval(() => {
var links = config.links;
if (!links) return;
var linkA = Object.values(links)
var lnks = linkA.map(c => c);
lnks.forEach(link => {
request(link.link, function (error, response) {
console.log(link)
console.error('error:', error);
console.log('statusCode:', response && response.statusCode);
if(error == null) {
var Data = {
"last_req": now,
"status_code": response.statusCode,
"error": false
}
var LogData = {
"id": link.link_id,
"last_req": now,
"status_code": response.statusCode,
"error": false
}
} else {
var Data = {
"last_req": now,
"status_code": 999,
"error": true
}
var LogData = {
"id": link.link_id,
"last_req": now,
"status_code": 999,
"error": true
}
}
db.add('requests.total', 1)
db.set(`requests.${link.link_id}`, Data)
log.push(`${link.link_id}`, LogData)
});
});
}, config.refresh_time)
// random string generator
function randomstring(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() *
charactersLength));
}
return result;
}
// Other
app.listen(port, () => {
console.log('[System Manager]: System running on port ' + port)
console.log('[System Manager]: https://github.com/vsl-dev')
})