forked from rb28z2/progress-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
124 lines (89 loc) · 2.8 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
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 * as common from "./common.js";
//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 = common.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();
}
if (config.enableRss) {
let rssClient = require("./rss.js");
rssClient.initRSS();
}
console.log("\nINIT COMPLETE\n".bold.magenta);
console.log(colors.grey("%s\n"), JSON.stringify(common.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("date-update", common.lastUpdated);
let flattenedStats = {shows: {}};
for (let [show, showStats] of Object.entries(common.stats.shows)) {
flattenedStats.shows[show] = common.flattenStats(showStats);
}
flattenedStats.roles = common.stats.roles;
socket.emit("init-stats", flattenedStats);
io.emit("update-users", io.engine.clientsCount);
socket.on("disconnect", socket => {
io.emit("update-users", io.engine.clientsCount);
});
});
if (config.enableHttp) {
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);
common.saveStats();
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
}));