This repository has been archived by the owner on Mar 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
150 lines (124 loc) · 3.61 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
/**
* Global Requirements
*/
const bodyParser = require('body-parser');
const config = require('./lib/_init').config;
const cookieParser = require('cookie-parser');
const db = require('./models');
const express = require('express');
const expressNunjucks = require('express-nunjucks');
//const favicon = require('serve-favicon');
const logger = require('morgan');
const passport = require('passport');
const path = require('path');
const RateLimit = require('express-rate-limit');
const session = require('express-session');
const sequelizeStore = require('connect-session-sequelize')(session.Store);
const steamStrategy = require('passport-steam').Strategy;
const app = express();
const server = require('http').createServer(app);
const isDev = app.get('env').trim() !== 'production';
if (isDev) {
app.use(logger('dev'));
}
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'njk');
expressNunjucks(app, {
watch: isDev,
noCache: isDev,
filters: {}
});
//app.use(favicon(path.join(__dirname, 'client', 'public', 'assets', 'favicon.ico')));
app.use('/assets', express.static(path.join(__dirname, 'assets')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(cookieParser());
const sessionParser = session({
secret: config('required.site.secretKey'),
saveUninitialized: true,
resave: true,
proxy: true,
store: new sequelizeStore({
db: db.sequelize,
expiration: 4 * 7 * 24 * 60 * 60 * 1000
})
});
app.use(sessionParser);
app.use(passport.initialize());
app.use(passport.session());
passport.use(new steamStrategy({
returnURL: '/auth/steam/callback',
realm: '/auth/steam/callback',
apiKey: config('required.steam.apiKey')
}, (identifier, profile, done) => {
const steamAvatar = profile._json.avatarfull.split('/').slice(-2).join('/');
db.Users.findOrCreate({
include: [
db.GameServers,
db.Matches,
db.Teams
],
where: {
steamId: +profile.id
}
}).spread((user) => user.updateAttributes({
steamAvatar,
steamUsername: profile.displayName
}).then(() => {
done(null, {
steamId: +profile.id,
steamAvatar,
steamUsername: profile.displayName
});
}).catch((err) => {
done(err);
}));
}));
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((user, done) => {
db.Users.find({
where: {
steamId: user.steamId
}
}).then((user) => {
done(null, user);
}).catch((err) => {
done(err);
});
});
const limiter = new RateLimit({
windowMs: 60 * 1000,
max: 250,
delayMs: 0
});
app.use(limiter);
app.use(require(path.join(__dirname, 'routes', 'index'))());
app.use(require(path.join(__dirname, 'routes', 'api'))());
app.use(require(path.join(__dirname, 'routes', 'auth'))(passport));
app.use(require(path.join(__dirname, 'routes', 'match'))());
app.use(require(path.join(__dirname, 'routes', 'server'))());
app.use(require(path.join(__dirname, 'routes', 'team'))());
app.use(require(path.join(__dirname, 'routes', 'user'))());
app.use((req, res, next) => {
if (!['GET', 'POST'].includes(req.method)) {
return res.status(400).end('Invalid HTTP Method!');
}
res.locals.IS_DEV = isDev;
res.locals.USER = req.user;
res.locals.PATH = req.url;
next();
});
app.use((err, req, res) => {
return res.status(err.status || 400).json({ message: err.message.toString() });
});
app.use((req, res, next) => {
return res.status(404).send('Sorry, Nothing at this URL.');
});
app.use((err, req, res) => {
return res.status(err.status || 500).send(`Sorry, unexpected error: ${err.message()}`);
});
module.exports = { app, server };