This repository has been archived by the owner on Feb 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 117
/
settings.js
110 lines (102 loc) · 3.13 KB
/
settings.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
// Module dependencies.
module.exports = function(app, configurations, express) {
var nconf = require('nconf');
var i18n = require('i18next');
var maxAge = 24 * 60 * 60 * 1000 * 28;
var nativeClients = require('./clients.json');
var RedisStore = require('connect-redis')(express);
var passport = require('passport');
var csrf = express.csrf();
nconf.argv().env().file({ file: 'local.json' });
// Configuration
var checkApiKey = function (req, res, next) {
if (req.body.apiKey && nativeClients.indexOf(req.body.apiKey) > -1) {
req.isApiUser = true;
}
next();
};
var clientBypassCSRF = function (req, res, next) {
if (req.isApiUser) {
next();
} else {
csrf(req, res, next);
}
};
i18n.init({
lng: nconf.get('locale'), // undefined detects user browser settings
supportedLngs: ['en', 'fr', 'es', 'ru'],
fallbackLng: 'en',
useCookie: false,
resGetPath: 'locales/__lng__.json'
});
i18n.registerAppHelper(app);
app.configure(function () {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.set('view options', { layout: false });
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
if (!process.env.NODE_ENV) {
app.use(express.logger('dev'));
}
app.use(express.static(__dirname + '/public'));
app.use(express.cookieParser());
app.use(express.session({
secret: nconf.get('session_secret'),
store: new RedisStore({ db: nconf.get('redis_db'), prefix: 'meatspacechat' }),
cookie: { maxAge: maxAge }
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(checkApiKey);
app.use(clientBypassCSRF);
app.use(function (req, res, next) {
res.locals.session = req.session;
if (!req.body.apiKey) {
res.locals.csrf = req.csrfToken();
} else {
res.locals.csrf = false;
}
if (!process.env.NODE_ENV) {
res.locals.debug = true;
} else {
res.locals.debug = false;
}
res.locals.analytics = nconf.get('analytics');
res.locals.appId = nconf.get('appId');
res.locals.analyticsHost = nconf.get('analyticsHost');
next();
});
app.use(i18n.handle);
app.enable('trust proxy');
app.locals.pretty = true;
app.use(function (req, res, next) {
// prevent framing by other sites
res.set('X-Frame-Options', 'SAMEORIGIN');
next();
});
app.use(app.router);
app.use(function (req, res, next) {
res.status(404);
res.render('404', { url: req.url, layout: false });
return;
});
app.use(function (req, res, next) {
res.status(403);
res.render('403', { url: req.url, layout: false });
return;
});
});
app.configure('development, test', function() {
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('prod', function() {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('500', { error: err, layout: false });
});
app.use(express.errorHandler());
});
return app;
};