-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.js
191 lines (165 loc) · 5.11 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
"use strict";
const express = require("express");
const path = require("path");
const favicon = require("serve-favicon");
const morganLogger = require("morgan");
const logger = require("./modules/logger");
const cookieParser = require("cookie-parser");
const bodyParser = require("body-parser");
const mongoose = require("./db").mongoose;
const passport = require("passport");
const LocalStrategy = require("passport-local").Strategy;
const DiscordStrategy = require("passport-discord").Strategy;
const session = require("express-session");
const helmet = require("helmet");
const MongoStore = require("connect-mongo")(session);
const IP_MONITOR = require("./middleware/ip-monitor");
//routes
const home = require("./routes/index");
const register = require("./routes/register");
const members = require("./routes/members");
const login = require("./routes/login");
const logout = require("./routes/logout");
const authcheck = require("./routes/authcheck");
const secure = require("./routes/secure");
const activate = require("./routes/activate");
const recover = require("./routes/recover");
const resetPassword = require("./routes/resetPassword");
const wing = require("./routes/wing");
const discordAuth = require("./routes/discord-auth");
const missions = require("./routes/companion-app/missions");
const combat = require("./routes/companion-app/combat");
const fleetCarriers = require("./routes/companion-app/fleet-carriers");
const app = express();
//IP Monitor
app.use(IP_MONITOR);
//for Let's Encrypt
app.use("/.well-known/acme-challenge/:fileid", function (req, res) {
res.sendFile(
path.join(__dirname, `.well-known/acme-challenge/${req.params.fileid}`)
);
});
//security headers
app.use(
helmet({
hsts: {
force: true,
maxAge: 2419200, //4 weeks
preload: true,
},
})
);
app.use(requireHTTPS);
app.use(function (req, res, next) {
res.header("X-powered-by", "Blood, sweat, and tears");
next();
});
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(morganLogger("dev"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
//mongo session store
app.use(
session({
store: new MongoStore({ mongooseConnection: mongoose.connection }),
secret: process.env.cookieSecret || require("./secrets").cookieSecret,
secure: true,
})
);
app.use(passport.initialize());
app.use(passport.session());
//mongoose passport config
mongoose.Promise = global.Promise;
require("./models/user").then((User) => {
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
});
//discord passport config
let scope = ["identify"];
passport.use(
new DiscordStrategy(
{
clientID:
process.env.discordClientID || require("./secrets").discord.clientID,
clientSecret:
process.env.discordClientSecret ||
require("./secrets").discord.clientSecret,
callbackURL:
process.env.discordCallback || require("./secrets").discord.callback,
resave: true,
saveUninitialized: true,
scope,
},
function (accessToken, refreshToken, profile, done) {
done(null, profile);
}
)
);
//allow CORS requests
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
//use secure connections
function requireHTTPS(req, res, next) {
var isAzure = req.get("x-site-deployment-id"),
isSsl = req.get("x-arr-ssl");
if (isAzure && !isSsl) {
return res.redirect("https://" + req.get("host") + req.url);
}
next();
}
//serve
app.use("/", home);
app.use("/secure", secure);
app.use("/api/register", register);
app.use("/api/members", members);
app.use("/api/login", login);
app.use("/api/logout", logout);
app.use("/api/authcheck", authcheck);
app.use("/api/activate", activate);
app.use("/api/recover", recover);
app.use("/api/resetpassword", resetPassword);
app.use("/api/wing", wing);
app.use("/discord", discordAuth);
//companion app routes
app.use("/api/combat", combat);
app.use("/api/missions", missions);
app.use("/api/fleet-carriers", fleetCarriers);
//checks auth level before sending these
app.use("/secure", secure);
//catchall redirect for angular html5Mode
app.use("/*", home);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error("Not Found");
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get("env") === "development") {
app.use(function (err, req, res, next) {
logger.log(err);
res.status(err.status || 500);
res.send(err.message);
});
}
// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
logger.log(err);
res.status(err.status || 500);
res.send(err.message);
});
//Discord bot
let kokBot = require("./modules/kok-bot");
//local variables
app.locals.guildID = "272391171416784897";
module.exports = app;