-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.js
134 lines (97 loc) · 3.03 KB
/
server.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
import config from "./config.js";
import fs from "fs";
//below block is to init stuff. nothing special here
import colors from "colors";
console.log("Initializing Express and HTTP stuff...".green);
import express from "express";
const app = express();
app.use(express.static("assets")); //deliver content in the 'assets' folder
let http = require("http")
.Server(app);
import {getStats, initIo, lastUpdated, validCommands, file } from "./common.js";
let stats = getStats();
//configure in HTTPS mode
if (config.httpsMode) {
console.log("HTTPS Mode".yellow);
http = require("https");
http = http.createServer({
key: fs.readFileSync(config.httpsKey),
cert: fs.readFileSync(config.httpsCert)
}, app);
}
console.log("Initializing Socket.io stuff...".green);
const io = initIo(http); // socket.io for realtime stuff
import jsonFile from "jsonfile"; //because i'm lazy
if (config.enableIrc) {
let ircClient = require("./irc.js");
ircClient.initIRC();
}
if (config.enableDiscord) {
let discordClient = require("./discord.js");
discordClient.initDiscord();
}
console.log("\nINIT COMPLETE\n".bold.magenta);
console.log(colors.grey("%s\n"), JSON.stringify(stats));
app.get("/", (req, res) => {
res.sendFile(`${__dirname}/index.html`);
});
app.get("/progressbar.min.js", (req, res) => {
res.sendFile(`${__dirname}/progressbar.min.js`);
});
io.on("connection", socket => {
console.log("Socket connection established. ID: ".grey, socket.id);
socket.emit("irc message", "Connected!");
socket.emit("date-update", lastUpdated);
//for each new client, update their stats (initial update)
for (let i = 0; i < validCommands.length; i++) {
let command = validCommands[i];
//console.log(command);
if (command !== "title" && command !== "episode") {
socket.emit("init-stats", {
"command": validCommands[i],
"value": stats[validCommands[i]] / 100
});
}
else {
socket.emit("init-stats", {
"command": validCommands[i],
"value": stats[validCommands[i]]
});
}
}
io.emit("update-users", io.engine.clientsCount);
socket.on("disconnect", socket => {
io.emit("update-users", io.engine.clientsCount);
});
});
if (config.httpsMode) {
http.listen(8443, () => {
console.log("Listening on port %s in HTTPS mode".bold.yellow, config.httpsPort);
});
}
else {
http.listen(config.port, () => {
console.log("Listening on port %s in HTTP mode".bold.yellow, config.port);
});
}
//Below stuff is all for clean exits and for uncaught exception handling
process.stdin.resume(); //so the program will not close instantly
function exitHandler({cleanup, exit}, err) {
if (cleanup) console.log("clean".red);
if (err) console.log(err.stack);
jsonFile.writeFileSync(file, stats);
console.log("Saving stats to disk".yellow);
if (exit) process.exit();
}
//do something when app is closing
process.on("exit", exitHandler.bind(null, {
cleanup: true
}));
//catches ctrl+c event
process.on("SIGINT", exitHandler.bind(null, {
exit: true
}));
//catches uncaught exceptions
process.on("uncaughtException", exitHandler.bind(null, {
exit: true
}));