forked from ruffrey/mailsac
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
358 lines (306 loc) · 10.5 KB
/
app.js
File metadata and controls
358 lines (306 loc) · 10.5 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
'use strict';
// Print a super long stack trace when not in production.
// This kills performance but is great for debugging.
if (process.env.NODE_ENV !== 'production') {
require('longjohn');
}
var debug = require('debug')('mailsac');
process.on('uncaughtException', function (err) {
debug('PROCESS UNCAUGHT EXCEPTION', err.message, err.stack);
// do restart immediately, to prevent massive system load
setTimeout(function () {
process.exit();
}, 300);
});
// Master process and workers
var config = require('config');
var cluster = require('cluster');
var WORKER_TOTAL = config.get('workers') || 1;
var child;
if (cluster.isMaster) {
debug('master', process.pid);
for (var i = 0; i < WORKER_TOTAL; i++) {
child = cluster.fork();
debug('forked worker', child.process.pid);
}
cluster.on('exit', function (worker, code, signal) {
debug('worker DIED', + worker.process.pid, code);
setTimeout(function () {
cluster.fork();
}, 300);
});
return;
}
// Worker / application
var express = require('express');
var fs = require('fs');
var path = require('path');
var nodemailer = require('nodemailer');
var moment = require('moment');
var fs = require('fs');
var SimpleSmtp = require('simplesmtp');
var jade = require('jade');
var url = require('url');
debug('------ Mailsac Worker Start ------', (process.env.NODE_ENV || 'development'), process.pid, '\n');
// Express middleware
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var RedisStore = require('connect-redis')(session);
var middleware = require('./lib/middleware');
var compression = require('compression');
// smtp
var smtp = require('./smtp-server');
var mailTransport = nodemailer.createTransport(config.get('smtp'));
if (process.env.NODE_ENV === 'production') {
process.on('uncaughtException', function (err) {
console.log('\n\n----\nPROCESS CAUGHT EXCEPTION', err);
console.log(err.stack);
// TODO: node >=0.10.6
if (err.message === 'read ECONNRESET') {
process.exit();
}
mailTransport.sendMail({
from: config.get('email.from'),
to: config.get('email.notify'),
subject: '[' + config.get('name') + ' event] CRASH',
text: err + '\n\n' + err.stack + '\n\n',
}, function (err) {
if (err) {
debug(err);
}
process.exit();
});
});
}
// app utilities
var appUtilities = require('./lib/app-utilities');
var schemas;
var models = {};
var app = express();
var mongoose = require('mongoose');
mongoose.connection.on('error', function (err) {
debug('mongo', err);
});
mongoose.connection.on('connected', function () {
debug('mongo', 'connected', mongoose.connection.host);
});
mongoose.connect(config.get('mongoURI'), function (err) {
if (err) {
debug('mongo', err);
}
schemas = require('./schemas')(mongoose);
// Serving static assets
app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(compression());
// Attaching app locals and utils to request
app.use(function (req, res, next) {
// Open for api access
// res.set('Access-Control-Allow-Origin', '*');
res.locals = req.locals || {};
res.locals.config = config;
res.locals.req = req;
res.locals.moment = moment;
// format is: `{ path: "/docs", text: "Documentation", icon: 'page' }`
res.locals.menu = [];
res.locals.links = [];
req.db = models;
req.email = mailTransport;
// internal notifications to admin for important events
req.debug = function (one, two, three) {
if (process.env.NODE_ENV !== 'production') {
return
}
var text = '[' + config.get('name') + '] ' + one + '\n\n';
if (two) {
text += '----\n' + JSON.stringify(two, null, 4) + '\n----\n';
}
if (three) {
text += '----\n' + JSON.stringify(three, null, 4) + '\n----\n';
}
req.email.sendMail({
from: config.get('email.from'),
to: config.get('email.notify'),
subject: '[' + config.get('name') + ' event] ' + one,
text: text
}, function (err) {
if (err) {
debug('internalEvent FAIL', err, one, two, three);
}
});
};
req.utils = res.locals.utils = appUtilities;
next();
});
app.use(require('less-middleware')(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
var sessionStore = new RedisStore();
app.use(session({
secret: config.get('sessionSecret'),
store: sessionStore,
saveUninitialized: true,
resave: true,
cookie: {
maxAge: config.get('cookieMaxAge')
}
}));
// Request parsers
app.use(bodyParser.json({
limit: config.get('maxUploadSize')
}));
app.use(bodyParser.urlencoded({
extended: false,
limit: config.get('maxUploadSize')
}));
app.use(cookieParser());
// view engine setup
app.set('view engine', 'jade');
// You can add custom views inside plugins by pushing more paths
// into viewFolders.
var viewFolders = [path.join(__dirname, 'views')];
//
// Loading Mailsac plugins
//
var pluginPath = path.join(__dirname, 'plugins');
var pluginVars = {
schemas: schemas,
config: config,
email: mailTransport,
app: app,
viewFolders: viewFolders
};
var menus = [];
var links = [];
var routes = [];
var hooks = {
afterCreateMessage: [function (savedMessage, parsedMessage) {
}]
};
var inits = [];
var includes = {
head: [],
footer: []
};
function run(fn) {
fn();
}
if (!config.get('disablePlugins') && fs.existsSync(pluginPath)) {
var bootstrapPlugin = function (file) {
// plugins must have `.plugin.js`
if (file.indexOf('.plugin') === -1) return;
var fullPath = './plugins/' + file;
debug('loading plugin', fullPath);
var plugin = require(fullPath)(pluginVars, app);
if (plugin.init) {
inits.push(plugin.init);
}
if (plugin.menu) {
menus.push(plugin.menu);
}
if (plugin.links) {
links.push(plugin.links);
}
if (plugin.routes) {
routes.push(plugin.routes);
}
if (plugin.hooks) {
Object.keys(plugin.hooks).forEach(function (h) {
hooks[h].push(plugin.hooks[h]);
});
}
var includesPath = path.resolve(__dirname, fullPath, 'includes');
if (fs.existsSync(includesPath)) {
fs.readdirSync(includesPath).forEach(function (jadeFile) {
includes[jadeFile.replace('.jade', '')].push(path.resolve(includesPath, jadeFile));
});
}
};
var pluginFiles = fs.readdirSync(pluginPath);
pluginFiles.forEach(bootstrapPlugin);
}
// view extensions
app.use(function loadIncludes(req, res, next) {
res.locals.includes = includes;
res.locals.renderFile = jade.renderFile;
next();
});
inits.forEach(run);
// After plugins were given the chance to add more view folders,
// set them here.
app.set('views', viewFolders);
menus.forEach(run);
links.forEach(run);
routes.forEach(run);
// Bind application routes
app.use('/', require('./routes/home'));
// End plugins
// After plugins have the chance to alter the schemas,
// initiate them.
Object.keys(schemas).forEach(function (name) {
models[name] = mongoose.model(name, schemas[name]);
});
// Error handling routes
// after plugins in case the plugins extend the app routes
app.use(middleware.fourOhFour);
app.use(middleware.errorHandler);
module.exports = app;
// Exception handling on the app
app.on('error', function (err) {
debug('EXPRESS APP ERROR', err, err.stack);
});
app.on('uncaughtException', function (err) {
debug('EXPRESS APP UNCAUGHT EXCEPTION', err, err.stack);
});
var server = app.listen(config.get('port'), function () {
debug(config.get('name') + ' is listening', config.get('port'));
});
server.on('error', function (err) {
debug('EXPRESS SERVER ERROR', err, err.stack);
});
server.on('uncaughtException', function (err) {
debug('EXPRESS SERVER UNCAUGHT EXCEPTION', err, err.stack);
});
if (config.get('forceSSL')) {
debug('starting ssl server');
(function () {
var https = require('https');
var sslServer = https.createServer({
key: fs.readFileSync(__dirname + '/keys/mailsac.com.key'),
cert: fs.readFileSync(__dirname + '/keys/mailsac.com.all.crt')
}, app);
sslServer.listen(443, function () {
debug('ssl server listening', 443);
});
})();
}
// SMTP server startup
if (config.get('smtpEnabled')) {
var normal_smtp_server = new SimpleSmtp.createServer(config.get('normal_smtp_opts'));
smtp(mongoose, normal_smtp_server, 25, hooks);
var secure_smtp_server = new SimpleSmtp.createServer(config.get('secure_smtp_opts'))
smtp(mongoose, secure_smtp_server, 587, hooks);
}
// message removal service
var recursiveRemoval = function () {
var cutoff = moment().add(-config.get('max_message_age_days'), 'days')._d;
debug('removal start', cutoff);
models.Message
.remove()
.where('savedBy', null)
.where('received').lt(cutoff)
.exec(function (err, count) {
if (err) {
debug('removal error', err);
return;
}
debug('removed', count.result);
setTimeout(recursiveRemoval, config.get('removal_interval'));
});
};
if (config.get('removal_interval')) {
recursiveRemoval();
}
});