-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (77 loc) · 2.52 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
const WEBHOOK_SECRET_KEY = 'fZnxW^n*fU9ChDjLAs43';
async function setupNgrok(port) {
const ngrok = require('ngrok');
// Additional ngrok options available at https://www.npmjs.com/package/ngrok
const url = await ngrok.connect({
addr: port
});
console.log(`Ngrok instance created: ${url}`);
return url;
}
async function setupExpress(port) {
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
// Needed to read `application/cloudevents+json` type
const cloudEventsParser = bodyParser.json({
type: 'application/*+json'
});
// Needed for initial subscription handshake
app.options('*', (req, res) => {
// Validate the handshake header is set
if (req.headers['webhook-request-origin'] === 'eventgrid.azure.net') {
// Valid the request contains our unique key
if (req.query.key === WEBHOOK_SECRET_KEY) {
res.set({
'WebHook-Allowed-Origin': 'eventgrid.azure.net',
'WebHook-Allowed-Rate': '100',
'Allow': 'POST'
});
console.log('Successfully handled CloudEvent Abuse Protection Handshake');
} else {
console.warn('Received a handshake request with an invalid key.');
}
}
res.end();
});
// Actual event handler
app.post('*', cloudEventsParser, (req, res) => {
// Validate the request contains our unique key
if (req.query.key === WEBHOOK_SECRET_KEY) {
switch (req.body.type) {
case 'com.blackbaud.constituent.emailaddress.change.v1':
console.log('Received handled email address event.');
console.log(req.body);
break;
default:
console.warn('Received an unhandled event.');
break;
}
} else {
console.warn('Received an event with an invalid key.');
}
res.sendStatus(200);
});
// Not needed. Just making ngrok appear happy.
app.get('*', (req, res) => {
res.send('Local instance successfully running.');
});
await app.listen(port);
console.log(`Local instance created: http://localhost:${port}`);
}
(async function setup() {
const args = require('minimist')(process.argv.slice(2));
const port = args.port || process.env.port || 5000;
if (args.ngrok !== false) {
console.log('Starting ngrok setup');
await setupNgrok(port);
} else {
console.log('Skipping ngrok setup');
}
if (args.express !== false) {
console.log('Starting Express setup');
await setupExpress(port);
} else {
console.log('Skipping Express setup');
}
})();