-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (104 loc) · 3.41 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
/**
* @typedef {express.Router} Express.Router
*/
const Azure = require("./src/azure"),
compression = require("compression"),
express = require("express"),
minify = require("./src/minify"),
morgan = require("morgan"),
morganExtensions = require("./src/extensions/morgan.extensions"),
Redis = require("@roncli/node-redis"),
Cache = Redis.Cache,
Discord = require("./src/discord"),
Log = require("./src/logging/log"),
Router = require("./src/router"),
settings = require("./settings");
// # #
// # #
// ### ### ### ### ### # # ###
// ## # # # # # # # # # #
// ## # # ## # # # # # #
// ### ## # # # ## ### ###
// #
/**
* Starts up the application.
*/
(async function startup() {
Log.log("Starting up...");
// Set title.
if (process.platform === "win32") {
process.title = "Overload Teams League";
} else {
process.stdout.write("\x1b]2;Overload Teams League\x1b\x5c");
}
// Setup Redis.
if (!settings.disableRedis) {
Redis.setup(settings.redis);
Redis.eventEmitter.on("error", (err) => {
Log.exception(`Redis error: ${err.message}`, {err: err.err});
});
await Cache.flush();
}
// Startup Azure.
Azure.startup();
// Startup Discord.
Discord.startup();
await Discord.connect();
// Setup express app.
const app = express();
// Get the router.
/** @type {Express.Router} */
let router;
try {
router = await Router.getRouter();
} catch (err) {
console.log(err);
return;
}
// Add morgan extensions.
morganExtensions(morgan);
// Initialize middleware stack.
app.use(morgan(":colorstatus \x1b[30m\x1b[0m:method\x1b[0m :url\x1b[30m\x1b[0m:newline Date :date[iso] IP :realip Time :colorresponse ms"));
app.use(compression());
// Setup public redirects.
app.use(express.static("public"));
// Setup Discord redirect.
app.get("/discord", (req, res) => {
res.redirect("https://ronc.li/otl-discord");
});
// Setup Google Calendar redirect.
app.get("/calendar", (req, res) => {
res.redirect("https://calendar.google.com/calendar/u/0?cid=cnJybmp2MzVrdHV1azI5NW43OGxkcGM3bm9AZ3JvdXAuY2FsZW5kYXIuZ29vZ2xlLmNvbQ");
});
// Setup JS/CSS handlers.
app.get("/css", minify.cssHandler);
app.get("/js", minify.jsHandler);
// 500 is an internal route, 404 it if it's requested directly.
app.use("/500", (req, res, next) => {
req.method = "GET";
req.url = "/404";
router(req, res, next);
});
// Setup dynamic routing.
app.use("/", router);
// 404 remaining pages.
app.use((req, res, next) => {
req.method = "GET";
req.url = "/404";
router(req, res, next);
});
// 500 errors.
app.use((err, req, res, next) => {
Log.exception("Unhandled error has occurred.", err);
req.method = "GET";
req.url = "/500";
router(req, res, next);
});
// Startup web server.
const port = process.env.PORT || settings.express.port;
app.listen(port);
console.log(`Web server listening on port ${port}.`);
}());
process.on("unhandledRejection", (reason) => {
Log.exception("Unhandled promise rejection caught.", reason);
});