-
Notifications
You must be signed in to change notification settings - Fork 1
/
master.js
136 lines (102 loc) · 3.21 KB
/
master.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
import config from "./config.js";
import { Slave } from "./slave.js";
import { authenticate, parseServer } from "./auth.js";
import { abort } from "./functions.js";
import { getStreamerData } from "./twitch.js";
import { resolveHistoricData, resolveTimestampData } from "./history-resolve.js";
import { warning } from "./colors.js";
const slaves = {};
async function terminateAll() {
const promises = [];
for (const server in slaves) {
const slave = slaves[server];
promises.push(slave.terminate());
}
await Promise.all(promises);
process.exit(0);
}
export function initSlaves(only = null) {
for (let i = 0; i < config.servers.length; i++) {
const server = config.servers[i];
if (only && server !== only) continue;
slaves[server] = new Slave(i + 1, server);
}
process.on("SIGTERM", async () => {
console.warn(warning("Terminating (SIGTERM)..."));
await terminateAll();
});
process.on("SIGINT", async () => {
console.warn(warning("Terminating (SIGINT)..."));
await terminateAll();
});
}
export function getSlaveData(server, type) {
const slave = slaves[server];
if (!slave) return false;
return slave.data(type);
}
export function initMasterRoutes(app) {
// Data route requires authentication
app.get("/socket/:server/data/:route/:options?", authenticate, async (req, resp) => {
const cluster = req.cluster,
slave = slaves[cluster];
if (!slave) return abort(resp, "Cluster not found");
await slave.get("data", req.params.route, req.params.options, resp);
});
// Static route does not require authentication
app.get("/socket/:server/static/:route", async (req, resp) => {
const server = parseServer(req.params.server),
slave = server ? slaves[server.cluster] : false;
if (!slave) return abort(resp, "Cluster not found");
await slave.get("static", req.params.route, "", resp);
});
// History route
app.get("/socket/:server/history/:license/:from/:till", authenticate, async (req, resp) => {
const params = req.params,
license = req.license,
server = req.server;
const from = "from" in params ? parseInt(params.from) : false,
till = "till" in params ? parseInt(params.till) : false;
if (!license || !from || from < 0 || !till || till < 0) {
return abort(resp, "Invalid request");
}
if (!config.storage) {
return abort(resp, "History is unavailable.");
}
try {
const data = await resolveHistoricData(server, license, from, till);
resp.json({
status: true,
data: data,
});
} catch (e) {
abort(resp, e.message);
}
});
// Timestamp route
app.get("/socket/:server/timestamp/:timestamp", authenticate, async (req, resp) => {
const params = req.params,
server = req.server;
const timestamp = "timestamp" in params ? parseInt(params.timestamp) : false;
if (!timestamp) return abort(resp, "Invalid request");
if (config.storage) {
return abort(resp, "Timestamp is unavailable.");
}
try {
const data = await resolveTimestampData(server, timestamp);
resp.json({
status: true,
data: data,
});
} catch (e) {
abort(resp, e.message);
}
});
// Misc data routes (no authentication)
app.get("/socket/:server/misc/twitch", (req, resp) => {
resp.json({
status: true,
data: getStreamerData(),
});
});
}