-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
94 lines (82 loc) · 2.93 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
var fs = require('fs'),
path = require('path'),
express = require('express'),
emailQueue = require('./lib/emailQueue'),
db = require("mongojs"),
Queue = emailQueue.Queue,
app = express(),
mailConfig = require('./lib/emailQueue/mailConfig.json'),
mongoConnection;
if (mailConfig.mongo){
var databaseUrl = process.env.EMAIL_QUEUE_MONGO_URL;
mongoConnection = db.connect(databaseUrl, mailConfig.mongo.collections);
}
var PUBLIC_DIR = path.dirname(__filename) + '/public',
SHARED_DIR = path.dirname(__filename) + '/shared';
var port = process.argv[2] || '8000',
secure = process.argv[3] === 'ssl',
sslOpts = { key: fs.readFileSync(SHARED_DIR + '/server.key'),
cert: fs.readFileSync(SHARED_DIR + '/server.crt')
},
protocol = secure ? 'https' : 'http',
queue = new Queue();
app.use(express.bodyParser());
app.set('view engine', 'ejs');
app.post('/email', function(req, res){
var body = req.param('body', null),
from = req.param('from', null),
to = req.param('to', null),
subject = req.param('subject', null),
delayTime = req.param('delayTime', null),
currentTimeStamp = new Date(),
envelope = { "body" : body,
"from" : from,
"to" : to,
"subject" : subject,
"delayTime" : delayTime,
"delayKey" : (Object.keys(queue) + currentTimeStamp.valueOf()),
"createdAt" : currentTimeStamp.toLocaleString()
};
envelope.delayObject = emailQueue.delay(delayTime, function(){
if (mailConfig.mongo){
mongoConnection.emails.save(envelope, function(err, saved) {
if( err || !saved ) {
console.log("email not saved");
} else {
console.log("email saved");
}
});
}
emailQueue.mail(envelope);
queue.stopByKey(envelope.delayKey);
});
queue[envelope.delayKey] = envelope;
res.send(String(envelope.delayKey));
});
app.get('/stop', function(req, res){
var delayKey = req.param('delayKey', null);
var foundKey = queue.stopByKey(delayKey);
if (foundKey){
res.render('success');
} else {
res.render('failure');
}
});
app.get('/email_list', function(req, res){
if (mailConfig.mongo){
mongoConnection.emails.find(function(err, docs) {
res.render('index', { emailList : queue, emailHistory: docs });
});
} else {
res.render('index', { emailList : queue, emailHistory : [] });
}
});
if (__filename == process.argv[1]) {
app.listen(Number(port));
if (!port && !secure) {
console.log('Usage: node emailQueue.js 8000 ssl # default port and optional security setting');
}
console.log('Listening on ' + port + " " + protocol);
} else {
exports.app = app;
}