-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
80 lines (65 loc) · 2.36 KB
/
index.js
File metadata and controls
80 lines (65 loc) · 2.36 KB
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
/**
* @typedef {import("http-errors").HttpError} HttpErrors.HttpError
*/
const compression = require("compression"),
Express = require("express"),
HotRouter = require("hot-router"),
Log = require("@roncli/node-application-insights-logger"),
path = require("path"),
util = require("util");
process.on("unhandledRejection", (reason) => {
Log.error("Unhandled promise rejection caught.", {err: reason instanceof Error ? reason : new Error(util.inspect(reason))});
});
// MARK: class Index
/**
* The primary class for the application.
*/
class Index {
// MARK: static async startup
/**
* Starts up the application.
* @returns {Promise<void>}
*/
static async startup() {
// Setup application insights.
if (process.env.APPINSIGHTS_CONNECTIONSTRING) {
Log.setupApplicationInsights(process.env.APPINSIGHTS_CONNECTIONSTRING, {application: "tis.roncli.com"});
}
console.log("Starting up...");
// Set title.
if (process.platform === "win32") {
process.title = "tis.roncli.com";
} else {
process.stdout.write("\x1b]2;tis.roncli.com\x1b\x5c");
}
// Setup express app.
const app = Express();
// Remove powered by.
app.disable("x-powered-by");
// Initialize middleware stack.
app.use(compression());
// Trust proxy to get correct IP from web server.
app.enable("trust proxy");
// Setup hot-router.
const router = new HotRouter.Router();
router.on("error", (data) => {
Log.error(data.message, {err: data.err, req: data.req});
});
try {
await router.setRoutes(path.join(__dirname, "web"), app, {hot: false});
} catch (err) {
Log.critical("Could not set up routes.", {err});
}
app.use((/** @type {HttpErrors.HttpError} */err, /** @type {Express.Request} */req, /** @type {Express.Response} */res, /** @type {Express.NextFunction} */next) => {
router.error(err, req, res, next);
});
// Startup webserver
const port = process.env.PORT || 8080;
app.listen(port);
Log.info(`Server PID ${process.pid} listening on port ${port}.`);
}
}
Index.startup().catch((err) => {
Log.error("Failed to start the application.", {err});
process.exit(1);
});