This repository has been archived by the owner on Dec 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
78 lines (72 loc) · 1.96 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
// setup logger
const logger = require('./logger')
// requires
const validator = require('email-addresses')
const app = require('express')()
app.use(require('helmet')())
// unsubscribe
const mailgun = require('mailgun-js')({
apiKey: process.env.MAILGUN_API_KEY,
domain: process.env.MAILGUN_DOMAIN,
})
const subscription = require('./subscription')(mailgun)
app.get('/unsubscribe/:email', function (req, res) {
if (req.params.email) {
const email = validator.parseOneAddress(req.params.email)
if (email === null) {
logger.info(`Got invalid email address : ${req.params.email}`)
}
else {
subscription.unsubscribe(email.address)
}
}
else {
logger.info('got no email address - ignoring request')
}
res.redirect('/unsubscribed')
})
app.get('/subscribe/:email', function (req, res) {
if (req.params.email) {
const email = validator.parseOneAddress(req.params.email)
if (email === null) {
logger.info(`Got invalid email address : ${req.params.email}`)
res.status(400).send('invalid email')
}
else {
subscription.subscribe(email.address)
res.status(200).send('Subscribed!')
}
}
else {
logger.info('got no email address - ignoring request')
res.status(400).send('missing email')
}
})
// emails
require('./email')
.then(sender => {
// send onboarding email
app.get('/onboard/:email/:name', function (req, res) {
if (req.params.email) {
sender.onboard(req.params.email, req.params.name)
res.status(200).send('okay')
}
else {
logger.info('got no email address - ignoring request')
res.status(400).send('missing email')
}
})
// readiness probe
app.get('/ready', function (req, res) {
res
.status(200)
.send('ok')
})
// start server
app.listen(process.env.PORT,
() => logger.info(`listening on ${process.env.PORT}`))
})
.catch(err => {
logger.error(err)
process.exit(0)
})