/
index.js
61 lines (53 loc) · 1.91 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
'use strict'
const {PubSub} = require('@google-cloud/pubsub')
const formPage = process.env.FORM_PAGE
const successPage = process.env.SUCCESS_PAGE
const errorPage = process.env.ERROR_PAGE
/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*
*/
exports.form = async(req, res) => {
if (req.method === 'POST') {
// Data to publish
let message = JSON.stringify(req.body)
const timestamp = new Date().toISOString()
const id = generateId()
const customAttributes = {'timestamp': timestamp, 'id': id}
console.log(`Backup json: ${message} / ${JSON.stringify(customAttributes)}`)
// Send message
try {
const dataBuffer = Buffer.from(message);
const pubSubClient = new PubSub();
const messageId = await pubSubClient
.topic('form-submissions')
.publish(dataBuffer, customAttributes)
console.log(`Message ${messageId} published (${JSON.stringify(customAttributes)}).`)
console.log(`Redirecting to success page: ${successPage}`)
res.redirect(`${successPage}?id=${id}`)
} catch(error) {
console.error("Error publishing message to pubsub", error)
console.error(new Error(`Backup json: ${message} / `))
console.log(`Redirecting to error page: ${errorPage}`)
res.redirect(`${errorPage}?id=${id}`)
}
} else {
// Bounce the user back to the form
console.log("Redirecting back to form page.")
res.redirect(formPage)
}
};
function generateId() {
// https://stackoverflow.com/questions/1349404/generate-random-string-characters-in-javascript
const length = 8
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
const charactersLength = characters.length;
var result = '';
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}