This repository has been archived by the owner on Jun 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
149 lines (122 loc) · 4.24 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
* @name left4status
* @author eartharoid <[email protected]>
* @website https://www.left4craft.org
* @license MIT
*/
const express = require('express');
const app = express();
const mysql = require('mysql');
const fs = require('fs');
const log = require('leekslazylogger');
const isReachable = require('is-reachable');
const config = require('./config');
const database = require('./database');
const services = require('./services.json');
const Discord = require('discord.js');
const hook = new Discord.WebhookClient(config.discord.webhook.id, config.discord.webhook.token);
const db = mysql.createConnection({
host: database.host,
// port: database.port,
user: database.user,
password: database.password,
database: database.name
});
// logger setup
log.setup({
logToFile: false,
timestamp: 'DD/MM/YY hh:mm:ss',
custom: {
db: {
title: 'MySQL',
colour: 'cyanBright'
}
}
});
/**
* Database
*/
db.connect((err) => {
// db.query(`DROP TABLE minecraft`) // reset database for development
if (err) { // if connection fails
log.warn('Could not connect to database');
log.warn(log.colour.bgYellowBright(log.color.black('CRITICAL: NO DATABASE CONNECTION - FATAL ERROR WILL LIKELY OCCUR')) + log.colour.bgBlack(''));
return log.error(err);
};
log.success(`Connected to database (${database.name}@${database.host})`);
db.query(`SELECT 1 FROM minecraft LIMIT 1;`, (err, result) => {
if (err) { // if table does not exist
log.warn(log.colour.bgYellowBright(log.color.black("'minecraft': TABLE IS MISSING: Please run 'node setup' to setup the database")) + log.colour.bgBlack(''));
return log.error(err);
};
log.success(`'minecraft' table exists`);
});
// db.query(`SELECT 1 FROM mc_query LIMIT 1;`, function (err, result) {
// if (err) { // if table does not exist
// log.warn(log.colour.bgYellowBright(log.color.black(''mc_query': TABLE IS MISSING: Please run 'node setup' to setup the database')) + log.colour.bgBlack(''));
// return log.error(err);
// };
// log.success(`'mc_query' table exists`)
// });
db.query(`SELECT 1 FROM websites LIMIT 1;`, (err, result) => {
if (err) { // if table does not exist
log.warn(log.colour.bgYellowBright(log.color.black("'websites': TABLE IS MISSING: Please run 'node setup' to setup the database")) + log.colour.bgBlack(''));
return log.error(err);
};
log.success(`'websites' table exists`);
});
});
// JSON Body Parser for POST reqs
app.use(express.json());
app.use(express.static('./public/'));
/**
* Workers
*/
const workers_dir = fs.readdirSync('./workers').filter(file => file.endsWith('.js'));
const workers = {};
for (const file of workers_dir) {
const worker = require(`./workers/${file}`);
workers[worker.name] = worker;
log.console(`[WORKERS] > Loaded '${worker.name}' worker`);
};
/**
* Routes
*/
const routes_dir = fs.readdirSync('./routes').filter(file => file.endsWith('.js'));
let runner = {
db: db,
workers: workers,
log: log,
hook: hook,
config: config
}
for (const file of routes_dir) {
const route = require(`./routes/${file}`);
app[route.method](route.path, (req, res) => route.execute(req, res, runner));
log.console(`[ROUTE MANAGER] > Loaded ${route.method.toUpperCase()} '${route.name}' route`);
};
/**
* Pingers
*/
// Bungee Proxy
// workers.queryBungee.run(runner);
// setInterval(() => {
// workers.queryBungee.run(runner);
// }, services.minecraft.servers.proxy.interval * 60000);
// websites
//for (site in services.websites) {
setInterval(async () => {
for (site in services.websites) {
workers.updateSite.run(runner, site, await isReachable(services.websites[site].host, { timeout: 15000 }));
}
}, config.ping_interval * 60000);
//};
// external APIs
for (api in services.external) {
workers[services.external[api].worker].run(runner);
setInterval(() => {
workers[services.external[api].worker].run(runner);
}, services.external[api].interval * 60000);
};
// start the server
app.listen(config.port, () => log.success(`HTTP server is listening on port ${config.port}`));