-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
133 lines (115 loc) · 3.88 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
// Load environment config variables
require('dotenv').config();
const createError = require('http-errors');
const express = require('express');
const session = require('express-session');
const path = require('path');
const expressLogger = require('morgan');
const fs = require('fs');
const bodyParser = require('body-parser');
const Passport = require('passport');
const PassportSteam = require('passport-steam');
const logger = require('./lib/logger');
const app = express();
app.use(session({ secret: process.env.SESSION_SECRET }));
// Initialize database connection
app.models = require('./models');
app.models.sequelize.sync().then(() => {
logger.info('Finished database initialization');
app.models.game.findAll().then((games) => {
const supportedGames = games.map(game => game.dataValues);
app.supportedGames = supportedGames;
logger.info(`Initialized ${supportedGames.length} supported games. ${supportedGames.map(game => game.fullName).join(', ')}`);
});
app.models.reason.findAll().then((reasons) => {
const supportedReasons = reasons.map(reason => reason.dataValues);
app.supportedReasons = supportedReasons;
logger.info(`Initialized ${supportedReasons.length} supported reasons. ${supportedReasons.map(reason => reason.reasonShort).join(', ')}`);
});
});
// view engine setup
app.set('view engine', 'pug');
app.set('views', path.join(__dirname, 'views'));
if (process.env.NODE_ENV !== 'test') {
app.use(expressLogger(':method :url :status :res[content-length] - :response-time ms', { stream: logger.stream }));
}
app.use(express.json());
app.use(express.urlencoded({
extended: false,
}));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
app.use(Passport.initialize());
app.use(Passport.session());
Passport.serializeUser((user, done) => {
logger.info(`Serialized user ${user.id}`);
done(null, user.id);
});
Passport.deserializeUser((id, done) => {
app.models.user.findByPk(id).then((result) => {
logger.info(`Deserialized user ${result.id}`);
done(null, result);
}).catch((e) => {
done(e);
});
});
Passport.use(new PassportSteam({
returnURL: `${process.env.HOSTNAME}/auth/steam/return`,
realm: process.env.HOSTNAME,
apiKey: process.env.STEAM_API_KEY,
},
(async (identifier, profile, done) => {
const {
steamid,
personaname,
// eslint-disable-next-line no-underscore-dangle
} = profile._json;
try {
let user = await app.models.user.findOrCreate({
where: {
steamId: steamid,
},
defaults: {
username: personaname,
steamId: steamid,
},
});
if (user[1]) {
logger.info(`New user registered via steam ${steamid}`);
}
user = user[0].get({ plain: true });
return done(null, user);
} catch (error) {
return done(error);
}
})));
require('./routes')(app);
// catch 404 and forward to error handler
app.use((req, res, next) => {
next(createError(404, 'This page does not exist.'));
});
// error handler
app.use((err, req, res) => {
logger.error(err);
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'dev' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
// Register helper functions
app.helpers = {};
fs
.readdirSync(`${__dirname}/helpers`)
.filter(file => (file.indexOf('.') !== 0) && (file.slice(-3) === '.js'))
.forEach((file) => {
const helper = require(`${__dirname}/helpers/${file}`); // eslint-disable-line
// Bind models to the helper function so we can access them
const boundHelper = helper.bind(helper, app.models);
app.helpers[file.replace('.js', '')] = boundHelper;
});
process.on('unhandledRejection', (reason, p) => {
logger.error(`Unhandled Rejection at: Promise ${p} because: ${reason}`);
});
module.exports = app;