-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.js
271 lines (221 loc) · 7.23 KB
/
app.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
require("dotenv").config();
const express = require("express");
const socket = require("socket.io");
const compression = require("compression");
/*
TODO - Could use again in the future, or delete
- Remove redis-server.exe with it
const expressSession = require('express-session');
let RedisStore = null;
let redisClient = null;
if(process.env.NODE_ENV !== 'production'){
const redis = require('redis');
RedisStore = require('connect-redis')(expressSession);
redisClient = redis.createClient();
}
*/
const cookieSession = require("cookie-session");
const cookieParser = require("cookie-parser");
const exphbs = require("express-handlebars");
const bodyParser = require("body-parser");
const passport = require("passport");
const passportSetup = require("./config/passport-setup");
const favicon = require("serve-favicon");
const authRoutes = require("./routes/auth-routes");
const adminRoutes = require("./routes/admin-routes");
const apiRoutes = require("./routes/api-routes");
const browseRoutes = require("./routes/browse-routes");
const profileRoutes = require("./routes/profile-routes");
const mloadsRoutes = require("./routes/mloads/mloads-routes");
const coreRoutes = require("./routes/core-routes");
const errorRoutes = require("./routes/error-routes");
const SocketConnections = require("./js/SocketConnections");
const keys = require("./config/keys");
const path = require("path");
const fs = require("fs");
const https = require("https");
const http = require("http");
const app = express();
app.use(compression());
app.use(express.json());
const port = process.env.PORT || 80;
// Rate Limiter
const rateLimiterFlexible = require("rate-limiter-flexible");
const rateOptions = {
points: 30, // X points
duration: 5, // Per X seconds
execEvenly: true,
clearExpiredByTimeout: true,
};
const rateLimiter = new rateLimiterFlexible.RateLimiterMemory(rateOptions);
// Handlebars
app.engine(
"handlebars",
exphbs({
defaultLayout: "main",
helpers: {
chooseSelection: function (checkedValue, currentValue) {
return checkedValue === currentValue ? " selected" : "";
},
section: function (name, options) {
if (!this._sections) this._sections = {};
this._sections[name] = options.fn(this);
return null;
},
ifEqual: function (a, b, options) {
if (a == b) {
return options.fn(this);
} else {
return options.inverse(this);
}
},
json: function (content) {
return JSON.stringify(content);
},
},
}),
);
app.set("view engine", "handlebars");
// Set static folder
app.use(express.static(path.join(__dirname, "public")));
// Body Parser
app.use(bodyParser.urlencoded({ extended: false }));
// Setup Express Session
/*
TODO - Could use again in the future, or delete
let sessionMiddleware = expressSession({
// In production use default MemoryStore, in development use RedisStore
store: (process.env.NODE_ENV === 'production') ? undefined : new RedisStore({ client: redisClient }),
secret: keys.session.expressSecret,
resave: false,
saveUninitialized: false,
cookie: {
secure: (process.env.NODE_ENV === 'production') ? true : false,
maxAge: 30 * 24 * 60 * 60 * 1000 // 30 days
}
});
app.use(sessionMiddleware);
*/
let sessionMiddleware = cookieSession({
name: "session",
keys: [keys.session.expressSecret],
// Cookie Options
maxAge: 30 * 24 * 60 * 60 * 1000, // 30 days
});
app.use(cookieParser(keys.session.expressSecret, {}), sessionMiddleware);
// Server Setup
let server = null;
if (process.env.NODE_ENV === "production") {
/*
const options = {
key: fs.readFileSync('/etc/letsencrypt/live/wanderersguide.app/privkey.pem', 'utf8'),
cert: fs.readFileSync('/etc/letsencrypt/live/wanderersguide.app/cert.pem', 'utf8'),
ca: fs.readFileSync('/etc/letsencrypt/live/wanderersguide.app/chain.pem', 'utf8')
};
server = https.createServer(options, app).listen(443);
*/
server = app.listen(port, () => {
console.log(`<< Wanderer's Guide >> Running on port ${port}`);
});
} else {
//server = http.createServer(app).listen(80);
server = app.listen(port, () => {
console.log(`<< Wanderer's Guide >> Running on port ${port}`);
});
}
// Socket IO
const io = socket(server, {
maxHttpBufferSize: 15e8, // 15 mb
pingTimeout: 60000, // 60 sec (should be above 30k to fix disconnection issues on Safari)
upgradeTimeout: 30000, // 20k or more
});
io.use(function (socket, next) {
sessionMiddleware(socket.request, socket.request.res, next);
});
// Initialize Passport
app.use(passport.initialize());
app.use(passport.session());
// Set FavIcon
app.use(favicon("./public/images/favicon.svg"));
// Databases
//const backgroundDB = require('./config/databases/background-database');
const contentDB = require("./config/databases/content-database");
// Testing Database Connections
/*
backgroundDB.authenticate()
.then(() => console.log('Background Database connected...'))
.catch(err => console.log('Database Connection Error: ' + err));*/
contentDB
.authenticate()
.then(() => console.log("Content Database connected..."))
.catch((err) => console.log("Database Connection Error: " + err));
// Security Headers
app.use(function (req, res, next) {
res.header(
"Strict-Transport-Security",
"max-age=31536000; includeSubDomains",
);
res.header("X-Frame-Options", "SAMEORIGIN");
res.header("X-Content-Type-Options", "nosniff");
res.header("Referrer-Policy", "no-referrer-when-downgrade");
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Methods",
"POST, PATCH, PUT, DELETE, OPTIONS",
);
res.header(
"Access-Control-Allow-Headers",
"Authorization, Origin, Content-Type, X-Auth-Token",
);
next();
});
app.options("/*", (_, res) => {
res.sendStatus(200);
});
// Rate Limiter
app.use(function (req, res, next) {
rateLimiter
.consume(req.ipAddress, 1) // -> consume 1 point
.then((rateLimiterRes) => {
// Points consumed
return next();
})
.catch((rateLimiterRes) => {
// Not enough points to consume
return res.sendStatus(429);
});
});
// Set up Routes
app.use("/auth", authRoutes);
app.use("/profile", profileRoutes);
app.use("/admin", adminRoutes);
app.use("/api", apiRoutes);
app.use("/browse", browseRoutes);
app.use("/mloads", mloadsRoutes);
app.use("/", coreRoutes);
app.use("*", errorRoutes); // 404 Route
io.setMaxListeners(24);
SocketConnections.basicConnection(io);
SocketConnections.sheetItems(io);
SocketConnections.sheetConditions(io);
SocketConnections.sheetCompanions(io);
SocketConnections.sheetGeneral(io);
SocketConnections.sheetSpells(io);
SocketConnections.builderBasics(io);
SocketConnections.builderAncestry(io);
SocketConnections.builderBackground(io);
SocketConnections.builderClass(io);
SocketConnections.builderFinalize(io);
SocketConnections.builderGeneral(io);
SocketConnections.builderGeneralProcessing(io);
SocketConnections.builderWSC(io);
SocketConnections.homebrewGeneral(io);
SocketConnections.homebrewBuilder(io);
SocketConnections.charDataManagement(io);
SocketConnections.homePage(io);
SocketConnections.browse(io);
SocketConnections.gmTools(io);
SocketConnections.remoteUpdate(io);
SocketConnections.buildPlanner(io);
SocketConnections.adminPanel(io);
SocketConnections.appAPI(io);