-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
web.js
126 lines (97 loc) · 3.12 KB
/
web.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
var express = require('express');
var morgan = require("morgan");
var septa_rr = require("./lib/septa/rr/main.js");
var septa_bus = require("./lib/septa/bus/main.js");
var util = require("util");
var 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
//
app.enable("trust proxy");
app.use(morgan(':remote-addr :method :url :status :res[content-length] - :response-time ms'));
app.set('view options', {
layout: true
});
//
// Are we running in production
//
var production = false;
//
// Check our environment.
//
if (app.get("env") != "production") {
console.log("Running in development mode!");
//production = true; // Debugging
};
if (app.get("env") == "production") {
console.log("Running in PRODUCTION mode!");
production = true;
};
//
// Associative array for all of our routes
//
var routes = {};
routes["main"] = require("./routes/main.js")(production);
routes["api"] = require("./routes/api.js")(production);
routes["api/status"] = require("./routes/api-status.js");
routes["api/rr"] = require("./routes/api-rr.js");
routes["api/rr/status"] = require("./routes/api-rr-status.js");
routes["api/rr/raw_data"] = require("./routes/api-rr-raw.js");
routes["api/bus"] = require("./routes/api-bus.js");
routes["api/bus/status"] = require("./routes/api-bus-status.js");
routes["api/bus/raw_data"] = require("./routes/api-bus-raw.js");
routes["echo"] = require("./routes/echo.js");
routes["faq"] = require("./routes/faq.js")(production);
//
// If hitting the API, include CORS headers.
//
app.use(function(req, res, next) {
var url = req.originalUrl;
if (url.match(/^\/api/)) {
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("/", routes["main"].go);
app.get("/api", routes["api"].go);
app.get("/api/status", routes["api/status"].go);
app.get("/api/rr", routes["api/rr"].go);
app.get("/api/rr/status", routes["api/rr/status"].go);
app.get("/api/rr/raw_data", routes["api/rr/raw_data"].go);
app.get("/api/bus", routes["api/bus"].go);
app.get("/api/bus/status", routes["api/bus/status"].go);
app.get("/api/bus/raw_data", routes["api/bus/raw_data"].go);
app.get("/echo", routes["echo"].go);
app.get("/faq", routes["faq"].go);
// Load Swagger API documentation
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
//
// Set this up, mostly for our favicon.
//
app.use(express.static(__dirname + '/public'));
//
// Set our Views directory for Jade.
//
app.set("views", __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.
//
var port = process.env.PORT || 5000;
app.listen(port, function() {
console.log("Listening on " + port);
});