forked from sdelements/lets-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
225 lines (191 loc) · 5.27 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//
// Let's Chat
//
'use strict';
var _ = require('lodash'),
fs = require('fs'),
colors = require('colors'),
express = require('express.oi'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
compression = require('compression'),
helmet = require('helmet'),
http = require('http'),
nunjucks = require('nunjucks'),
mongoose = require('mongoose'),
migroose = require('./migroose'),
MongoStore = require('connect-mongo')(express.session),
all = require('require-tree');
var psjon = require('./package.json'),
settings = require('./app/config'),
httpEnabled = settings.http && settings.http.enable,
httpsEnabled = settings.https && settings.https.enable;
var auth = require('./app/auth/index'),
models = all('./app/models'),
middlewares = all('./app/middlewares'),
controllers = all('./app/controllers'),
core = require('./app/core/index'),
app;
//
// express.oi Setup
//
if (httpsEnabled) {
app = express().https({
key: fs.readFileSync(settings.https.key),
cert: fs.readFileSync(settings.https.cert)
}).io();
} else {
app = express().http().io();
}
if (settings.env === 'production') {
app.set('env', settings.env);
app.set('json spaces', undefined);
app.enable('view cache');
}
// Session
var sessionStore = new MongoStore({
url: settings.database.uri,
autoReconnect: true
});
// Session
var session = {
key: 'connect.sid',
secret: settings.secrets.cookie,
store: sessionStore,
cookie: { secure: httpsEnabled },
resave: false,
saveUninitialized: true
};
// Set compression before any routes
app.use(compression({ threshold: 512 }));
app.use(cookieParser());
app.io.session(session);
auth.setup(app, session, core);
// Security protections
app.use(helmet.crossdomain());
app.use(helmet.frameguard());
app.use(helmet.hidePoweredBy());
app.use(helmet.ieNoOpen());
app.use(helmet.noSniff());
app.use(helmet.xssFilter());
app.use(helmet.hsts({
maxAge: 31536000,
includeSubdomains: true,
force: httpsEnabled,
preload: true
}));
app.use(helmet.contentSecurityPolicy({
defaultSrc: ['\'none\''],
connectSrc: ['*'],
scriptSrc: ['\'self\'', '\'unsafe-eval\''],
styleSrc: ['\'self\'', 'fonts.googleapis.com', '\'unsafe-inline\''],
fontSrc: ['\'self\'', 'fonts.gstatic.com'],
mediaSrc: ['\'self\''],
objectSrc: ['\'self\''],
imgSrc: ['*']
}));
var bundles = {};
app.use(require('connect-assets')({
paths: [
'media/js',
'media/less',
// 'media/img',
// 'media/font',
],
helperContext: bundles,
build: settings.env === 'production',
fingerprinting: settings.env === 'production',
servePath: 'media/dist'
}));
// Public
app.use('/media', express.static(__dirname + '/media', {
maxAge: '364d',
}));
// Templates
var nun = nunjucks.configure('templates', {
autoescape: true,
express: app,
tags: {
blockStart: '<%',
blockEnd: '%>',
variableStart: '<$',
variableEnd: '$>',
commentStart: '<#',
commentEnd: '#>'
}
});
nun.addFilter('js', bundles.js);
nun.addFilter('css', bundles.css);
// HTTP Middlewares
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
//
// Controllers
//
_.each(controllers, function(controller) {
controller.apply({
app: app,
core: core,
settings: settings,
middlewares: middlewares,
models: models,
controllers: controllers
});
});
//
// Mongo
//
mongoose.connection.on('error', function (err) {
throw new Error(err);
});
mongoose.connection.on('disconnected', function() {
throw new Error('Could not connect to database');
});
//
// Go Time
//
function startApp() {
var port = httpsEnabled && settings.https.port ||
httpEnabled && settings.http.port;
var host = httpsEnabled && settings.https.host ||
httpEnabled && settings.http.host || '0.0.0.0';
if (httpsEnabled && httpEnabled) {
// Create an HTTP -> HTTPS redirect server
var redirectServer = express();
redirectServer.get('*', function(req, res) {
var urlPort = port === 80 ? '' : ':' + port;
res.redirect('https://' + req.hostname + urlPort + req.path);
});
http.createServer(redirectServer)
.listen(settings.http.port || 5000, host);
}
app.listen(port, host);
//
// XMPP
//
if (settings.xmpp.enable) {
var xmpp = require('./app/xmpp/index');
xmpp(core);
}
var art = fs.readFileSync('./app/misc/art.txt', 'utf8');
console.log('\n' + art + '\n\n' + 'Release ' + psjon.version.yellow + '\n');
}
mongoose.connect(settings.database.uri, function(err) {
if (err) {
throw err;
}
migroose.needsMigration(function(migrationRequired) {
if (migrationRequired) {
console.log('Database migration required'.red);
console.log('Ensure you backup your database first.');
console.log('');
console.log(
'Run the following command: ' + 'npm run-script migrate'.yellow
);
return process.exit();
}
startApp();
});
});