-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathweb.mjs
More file actions
132 lines (111 loc) · 4.13 KB
/
web.mjs
File metadata and controls
132 lines (111 loc) · 4.13 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
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
// web.mjs
import express from "express";
import morgan from "morgan";
import path from "node:path";
import { fileURLToPath } from "node:url";
// Load a utility function
import { isTrustProxyEnabled } from "./lib/util.mjs";
import { isProductionEnv } from "./lib/util.mjs";
// Load our SEPTA-related modules
import { boot as septa_rr_boot } from "./lib/septa/rr/main.mjs";
import { boot as septa_bus_boot } from "./lib/septa/bus/main.mjs";
// Routes
import { go as route_main_go } from "./routes/main.mjs";
import { go as route_api_go } from "./routes/api.mjs";
import { go as route_api_status_go } from "./routes/api-status.mjs";
import { go as route_api_rr_go } from "./routes/api-rr.mjs";
import { go as route_api_rr_status_go } from "./routes/api-rr-status.mjs";
import { go as route_api_rr_raw_go } from "./routes/api-rr-raw.mjs";
import { go as route_api_bus_go } from "./routes/api-bus.mjs";
import { go as route_api_bus_status_go } from "./routes/api-bus-status.mjs";
import { go as route_api_bus_raw_go } from "./routes/api-bus-raw.mjs";
import { go as route_debug_go } from "./routes/debug.mjs";
import { go as route_echo_go } from "./routes/echo.mjs";
import { go as route_faq_go } from "./routes/faq.mjs";
import { go as route_version_go } from "./routes/version.mjs";
import { createRequire } from "node:module";
const require = createRequire(import.meta.url);
const swaggerUi = require("swagger-ui-express");
const swaggerDocument = require("./swagger.json");
// Recreate __dirname in ESM
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
//
// Trust the proxy to provide the proper source IP as per:
//
// https://stackoverflow.com/questions/27588434/logging-with-morgan-only-shows-127-0-0-1-for-remote-addr-in-nodejs
//
if (isTrustProxyEnabled()) {
console.log("TRUST_PROXY env variable is set, enabling trust proxy in Express.");
app.enable("trust proxy");
}
app.use(morgan(":remote-addr :method :url :status :res[content-length] - :response-time ms"));
app.set("view options", { layout: true });
//
// If hitting the API, include CORS headers.
//
app.use((req, res, next) => {
const url = req.originalUrl;
if (/^\/api/.test(url)) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
}
next();
});
app.get("/", route_main_go);
app.get("/api", route_api_go);
app.get("/api/status", route_api_status_go);
app.get("/api/rr", route_api_rr_go);
app.get("/api/rr/status", route_api_rr_status_go);
app.get("/api/rr/raw_data", route_api_rr_raw_go);
app.get("/api/bus", route_api_bus_go);
app.get("/api/bus/status", route_api_bus_status_go);
app.get("/api/bus/raw_data", route_api_bus_raw_go);
app.get("/echo", route_echo_go);
app.get("/faq", route_faq_go);
app.get("/version", route_version_go);
if (!isProductionEnv()) {
app.get("/debug", route_debug_go);
}
// Load Swagger API documentation
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
//
// Set this up, mostly for our favicon.
//
app.use(express.static(path.join(__dirname, "public")));
//
// Have an error handler of last resort, in case we miss catching an error further down.
//
app.use((err, req, res, next) => {
console.error(err);
let url = "https://github.com/dmuth/IsSeptaFcked/issues"
let stack = err?.stack || String(err);
let message = `Ah jeez, we have an uncaught error. Please go to ${url} and report the issue so I can fix it. Here's the error, BTW:\n${stack}`;
res.status(500).send(message);
});
//
// Set our Views directory for Jade.
//
app.set("views", path.join(__dirname, "views"));
//
// Don't minify the HTML.
//
app.set("view options", { pretty: true });
//
// Start up the SEPTA sub-system, specifically fetching from the API.
//
septa_rr_boot();
septa_bus_boot();
//
// Actually start listening.
//
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`Listening on ${port}`);
});
//setInterval(() => {
// const m = process.memoryUsage();
// console.log(`Memory usage: ${m.rss} bytes`);
//}, 1 * 1000).unref();