forked from metabrainz/bookbrainz-site
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
158 lines (127 loc) · 3.94 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
/*
* Copyright (C) 2014-2015 Ben Ockmore
* 2015 Sean Burke
* 2015 Leo Verto
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
'use strict';
const path = require('path');
const express = require('express');
const favicon = require('serve-favicon');
const logger = require('morgan');
const bodyParser = require('body-parser');
const session = require('express-session');
const RedisStore = require('connect-redis')(session);
const staticCache = require('express-static-cache');
const Promise = require('bluebird');
Promise.longStackTraces();
const auth = require('./src/server/helpers/auth');
const config = require('./src/server/helpers/config');
const bbws = require('./src/server/helpers/bbws');
const status = require('http-status');
const git = require('git-rev');
// Initialize application
const app = express();
// Set up jade as view engine
app.set('views', path.join(__dirname, 'templates'));
app.set('view engine', 'jade');
app.locals.basedir = app.get('views');
require('node-jsx').install({
extension: '.jsx'
});
app.set('trust proxy', config.site.proxyTrust);
app.use(favicon(path.join(__dirname, 'static/images/icons/favicon.ico')));
if (app.get('env') !== 'testing') {
app.use(logger('dev'));
}
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(staticCache(path.join(__dirname, 'static/js'), {
buffer: true
}));
app.use(express.static(path.join(__dirname, 'static')));
app.use(session({
store: new RedisStore({
host: config.session.redis.host,
port: config.session.redis.port,
ttl: config.session.redis.ttl
}),
cookie: {
secure: config.session.secure
},
secret: config.session.secret,
resave: false,
saveUninitialized: false
}));
auth.init(app);
let siteRevision = null;
git.short((revision) => {
siteRevision = revision;
});
/* Add middleware to set variables used for every rendered route. */
app.use((req, res, next) => {
res.locals.user = req.user;
res.locals.respoitoryUrl = 'https://github.com/bookbrainz/bookbrainz-site/';
res.locals.siteRevision = siteRevision;
// Get the latest count of messages in the user's inbox.
if (req.session && req.session.bearerToken) {
return bbws.get('/message/inbox/', {
accessToken: req.session.bearerToken
})
.then((list) => {
res.locals.inboxCount = list.objects.length;
})
.catch((err) => {
console.log(err.stack);
res.locals.inboxCount = 0;
})
.finally(next);
}
res.locals.inboxCount = 0;
next();
});
// Set up routes
require('./src/server/routes')(app);
// Catch 404 and forward to error handler
app.use((req, res, next) => {
const err = new Error('Not Found');
err.status = status.NOT_FOUND;
next(err);
});
// Error handlers
/* Development error handler; displays stacktrace to user */
if (app.get('env') === 'development') {
app.use((err, req, res) => {
console.log(`Internal Error. Message: ${err.message} Stacktrace...`);
console.log(err.stack);
res.status(err.status || status.INTERNAL_SERVER_ERROR);
res.render('error', {
message: err.message,
error: err
});
});
}
/* Production error handler; stacktrace is omitted */
app.use((err, req, res) => {
res.status(err.status || status.INTERNAL_SERVER_ERROR);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;