-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
111 lines (97 loc) · 2.42 KB
/
index.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
const env = process.env;
const mailgunConfig = {
publicApiKey: env.MAILGUN_PUBLIC_API_KEY,
apiKey: env.MAILGUN_API_KEY,
domain: env.MAILGUN_DOMAIN
};
const list = env.MAILGUN_MAILING_LIST;
for(let key in mailgunConfig) {
if(!mailgunConfig[key]) {
console.log(`Make sure you provided all the required environment variables. Failed to detect ${key}.`);
return false;
}
}
if(!list) {
console.log('Make sure you provided the MAILGUN_MAILING_LIST environment variable.');
return false;
}
const base = env.APP_BASE || '/';
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const mailgun = require('mailgun-js')(mailgunConfig);
const app = express();
const mailing = express();
const whitelist = env.WHITELIST_DOMAINS ?
env.WHITELIST_DOMAINS.split(',').map(s => s.trim()) :
false;
const corsOptions = {
origin: function(origin, callback) {
if(!whitelist) {
callback(null, true);
} else {
if(whitelist.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
}
}
};
app.use(cors(corsOptions));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const validateName = function(req, res, next) {
if(req.body.name) {
next();
} else {
res.status(400).send('name_empty');
}
}
const validateEmail = function(req, res, next) {
if(req.body.email) {
mailgun.validate(req.body.email, function(err, body) {
if(body && body.is_valid) {
next();
} else {
res.status(400).send('email_invalid');
}
});
} else {
res.status(400).send('email_empty');
}
};
const parseSubscription = function (req, res, next) {
const { name, email, ...vars } = req.body;
req.member = {
name: name,
address: email,
vars: vars
};
next();
};
mailing.post('/validate', validateEmail, function(req, res) {
res.send('ok');
});
mailing.post('/subscribe',
validateName,
validateEmail,
parseSubscription,
function(req, res) {
mailgun
.lists(list)
.members()
.add({members: [req.member], subscribed: true}, function(err, body) {
if(err) {
res.status(400).send(err);
} else {
res.send(body);
}
});
}
);
app.use(base, mailing);
const port = env.PORT || 3000;
app.listen(port, function() {
console.log(`Running on port ${port}`);
});