forked from PhlexPlexico/G5API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
203 lines (182 loc) · 5.57 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
const createError = require("http-errors");
const express = require("express");
const cookieParser = require("cookie-parser");
const logger = require("morgan");
const helmet = require("helmet");
const cors = require("cors");
const morgan = require("morgan");
//Route Files
const indexRouter = require("./routes/index");
const leaderboardRouter = require("./routes/leaderboard");
const legacyAPICalls = require("./routes/legacy/api");
const matchesRouter = require("./routes/matches/matches");
const matchServerRouter = require("./routes/matches/matchserver");
const mapstatsRouter = require("./routes/mapstats");
const playerstatsRouter = require("./routes/playerstats");
const seasonsRouter = require("./routes/seasons");
const serversRouter = require("./routes/servers");
const teamsRouter = require("./routes/teams");
const usersRouter = require("./routes/users");
const vetoesRouter = require("./routes/vetoes");
const vetosidesRouter = require("./routes/vetosides");
const mapListRouter = require("./routes/maps");
//End Route Files
const swaggerUi = require("swagger-ui-express");
const swaggerJSDoc = require("swagger-jsdoc");
const passport = require("./utility/auth");
const bearerToken = require("express-bearer-token");
const config = require("config");
const session = require("express-session");
const redis = require("redis");
const app = express();
app.use(logger("dev"));
app.use(express.raw({type: 'application/octet-stream', limit: "2gb"}));
app.use(express.json({limit: "512kb"}));
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use("/demo", express.static("public"));
app.use("/static/img/logos", express.static("public/img/logos"));
// Security defaults with helmet
app.use(helmet());
if(config.get("server.useRedis")){
// Messy but avoids any open file handles.
const redisClient =
process.env.NODE_ENV !== "test"
? redis.createClient({
password: config.get(process.env.NODE_ENV + ".redisPass"),
})
: require("redis-mock").createClient();
const redisStore = require("connect-redis")(session);
redisClient.on("error", (err) => {
console.log("Redis error: ", err);
});
const redisCfg = {
host: config.get(process.env.NODE_ENV + ".redisHost"),
port: config.get(process.env.NODE_ENV + ".redisPort"),
client: redisClient,
ttl: config.get(process.env.NODE_ENV + ".redisTTL"),
};
app.use(
session({
secret: config.get("server.sharedSecret"),
name: "G5API",
resave: false,
saveUninitialized: true,
store: new redisStore(redisCfg),
cookie: { maxAge: 3600000 },
})
);
} else {
app.use(
session({
secret: config.get("server.sharedSecret"),
name: "G5API",
resave: false,
saveUninitialized: true,
cookie: { maxAge: 3600000 },
})
);
}
app.use(passport.initialize());
app.use(passport.session());
app.use(bearerToken());
// enabling CORS for all requests
app.use(
cors({
origin: config.get("server.clientHome"), // allow to server to accept request from different origin
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
credentials: true, // allow session cookie from browser to pass through
})
);
// adding morgan to log HTTP requests
app.use(morgan("combined"));
// swagger UI
const options = {
definition: {
openapi: "3.0.0", // Specification (optional, defaults to swagger: '2.0')
info: {
title: "G5API", // Title (required)
version: "1.3.0", // Version (required)
},
},
// Path to the API docs
apis: [
"./routes/leaderboard.js",
"./routes/legacy/api.js",
"./routes/matches/matches.js",
"./routes/matches/matchserver.js",
"./routes/mapstats.js",
"./routes/playerstats.js",
"./routes/seasons.js",
"./routes/servers.js",
"./routes/teams.js",
"./routes/users.js",
"./routes/vetoes.js",
"./routes/vetosides.js",
],
};
const swaggerSpec = swaggerJSDoc(options);
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerSpec));
// END API SETUP
// Begin Routes
app.use("/", indexRouter);
app.use("/users", usersRouter);
app.use("/teams", teamsRouter);
app.use("/servers", serversRouter);
app.use("/vetoes", vetoesRouter);
app.use("/vetosides", vetosidesRouter);
app.use("/matches", matchesRouter, matchServerRouter);
app.use("/mapstats", mapstatsRouter);
app.use("/playerstats", playerstatsRouter);
app.use("/seasons", seasonsRouter);
app.use("/match", legacyAPICalls);
app.use("/leaderboard", leaderboardRouter);
app.use("/maps", mapListRouter);
//END ROUTES
// Steam API Calls.
app.get(
"/auth/steam",
passport.authenticate("steam", { failureRedirect: "/" }),
(req, res) => {
res.redirect("/");
}
);
app.get(
"/auth/steam/return",
(req, res, next) => {
req.url = req.originalUrl;
next();
},
passport.authenticate("steam", { failureRedirect: "/" }),
(req, res) => {
if (process.env.NODE_ENV == "test") {
res.redirect("/");
} else {
res.redirect(config.get("server.clientHome"));
}
}
);
app.get("/logout", function (req, res) {
req.logout();
res.redirect("/");
});
// END Steam API Calls.
// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404));
});
// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
// render the error page
res.status(err.status || 500);
res.json({ error: err.message });
});
if(config.get("server.useRedis")){
process.on("exit", function () {
redisClient.end();
});
}
module.exports = app;