-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathforwarder.js
51 lines (41 loc) · 1.25 KB
/
forwarder.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
var follow = require('follow');
var request = require('request');
module.exports = function(db_url, handler_url) {
function isSuccessCode(code) {
return code >= 200 && code < 300;
}
function forwardHook(doc) {
var url = doc.req.query.notify_url || doc.req.query.handler_url || handler_url;
if (!url) {
console.log("No handler_url defined for webhook id: " + doc._id);
return;
}
request({
url: url,
method: doc.req.method,
body: doc.req.body,
headers: {
'Content-type': doc.req.headers["Content-Type"]
}
}, function (error, response, body) {
if (error) {
console.log("Error forwarding hook: " + error);
} else if (!isSuccessCode(response.statusCode)) {
console.log("Error forwarding hook: server returned " + response.statusCode);
} else {
console.log("Forwarded webhook id: " + doc._id);
}
});
}
follow({db: db_url, include_docs:true, since: 'now'}, function(error, change) {
if (error) {
console.log("Error following changes: " + error);
} else {
var doc = change.doc;
if (doc.type === 'webhook') {
forwardHook(doc);
}
}
});
console.log("Hookforward is listening for changes...");
}