-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
82 lines (71 loc) · 2.53 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
var fs = require('fs'),
path = require('path'),
express = require('express'),
emailQueue = require('./lib/emailQueue'),
app = express();
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 = [];
var stopByKey = function(key){
keyFound = false;
for (var i = 0; i < queue.length; i++){
var envelope = queue[i];
if (keyFound) {
envelope.queueIndex --;
} else if (envelope.delayKey == key){
keyFound = true;
clearTimeout(queue[envelope.queueIndex].delayObject);
queue.splice(i, 1);
}
}
};
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),
envelope = { 'body' : body, 'from' : from, 'to' : to, 'subject' : subject, 'delayTime' : delayTime, 'queueIndex' : queue.length, 'delayKey' : (queue.length + new Date().valueOf()) };
envelope.delayObject = emailQueue.delay(delayTime, function(){
emailQueue.mail(envelope);
stopByKey(envelope.delayKey);
});
queue.push(envelope);
res.send(String(envelope.delayKey));
});
app.get('/stop_by_idx', function(req, res){
var queueIndex = req.param('queueIndex', null);
clearTimeout(queue[queueIndex].delayObject);
queue.splice(queueIndex, 1);
var updateArray = queue.slice(queueIndex, queue.length)
for(idx in updateArray){
var notRemovedElement = updateArray[idx];
notRemovedElement.queueIndex --;
}
res.redirect('/email_list');
});
app.get('/stop', function(req, res){
var delayKey = req.param('delayKey', null);
var foundKey = stopByKey(delayKey);
if (foundKey){
res.render('success');
} else {
res.render('failure');
}
});
app.get('/email_list', function(req, res){
res.render('index', { emailList : queue, test : "test" });
});
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);