This repository has been archived by the owner on Jun 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
99 lines (92 loc) · 2.84 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
95
96
97
98
99
require('dotenv').config()
const serverless = require('serverless-http');
const express = require('express');
const bodyParser = require('body-parser');
const { sign, validate } = require('./sign');
const s3Log = require('./s3-log');
const rp = require('request-promise');
const getSecrets = function() {
return new Promise((resolve, reject) => {
if (!process.env.ACTION_NETWORK_API_KEY) {
const slscrypt = require('slscrypt')
slscrypt.get('ACTION_NETWORK_API_KEY').then((key) => {
process.env.ACTION_NETWORK_API_KEY = key
resolve()
})
} else {
resolve()
}
})
}
const app = express()
app.use(bodyParser.json({ strict: false }));
app.use(bodyParser.urlencoded({ extended: false }));
app.get('/', function (req, res) {
res.send('Hello World!')
})
app.post('/sign/:formId/wait', function(req, res, wait) {
console.log("doing the full wait version...")
getSecrets()
.then(() => {
sign(req.body, req.params.formId)
.then(() => {
if (process.env.SAVE_SUCCESS) {
s3Log(req.body, `success/${req.body.email}.json`)
.then(() => {
console.log(`Saved success/${req.body.email} to s3`)
res.sendStatus(201)
})
.catch(err => {
console.log(`Error saving success to s3: ${err}}`)
res.sendStatus(500)
})
} else {
res.sendStatus(201)
}
})
.catch(err => {
console.log("error writing:", err)
console.log("Writing failed body to s3")
s3Log(req.body, `error/${req.body.email}.json`)
.then(s3Res => {
console.log(`Saved error/${req.body.email} to s3`)
res.status(err && err.status || 500)
if (err.message) {
res.json(err)
} else {
res.end()
}
})
.catch(s3Error => {
console.log(`Error saving error to s3: ${s3Error}`)
res.json(s3Error)
})
})
})
})
app.post('/sign/:formId', function(req, res, next) {
console.log(`Signing petition for ${JSON.stringify(req.body)}`)
// if the input is bad, return immediately
const validationResult = validate(req.body)
if (validationResult.error) {
res.status(401)
return res.json(validationResult)
} else {
// fire off the request that will actually process, then return
const fullUrl = req.protocol + '://' + req.get('host') + (process.env.PATH_PREFIX || '') + req.originalUrl;
const waitUri = fullUrl + '/wait'
console.log("Sending new request to", waitUri)
const result = rp({
uri: waitUri,
method: 'POST',
body: req.body,
json: true
}).catch(err => {
console.log("Error calling wait endpoint", err)
})
setTimeout(function() {
res.sendStatus(201)
}, 100)
}
})
module.exports = app