-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvici.actionRoute.js
73 lines (65 loc) · 2.18 KB
/
vici.actionRoute.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
var _ = require('underscore')
var status = require('hapi-status')
var executors = {
script: require('./vici.script.exec.js'),
afterExec: require('./vici.after.exec.js')
}
module.exports = function (config) {
return _.map(config.actionYmls, function (action) {
return {
method: action.method || 'POST',
path: '/do/' + encodeURI(action.name),
handler: function (request, reply) {
var isAuth = _isSecretPresentInHeader(request, config.viciSecret) ||
_isSecretPresentInQuery(request, config.viciSecret)
if (!isAuth) {
return status.unauthorized(reply, 'Authorization failed.')
}
var on_success = _.map(action.on_success, function (action) {
var url = 'http://localhost:' + config.viciPort + '/do/' + action.do
return {
url: url,
method: action.method || 'POST',
headers: {
'x-vici-secret': config.viciSecret
},
payload: action.payload || {}
}
})
var on_failure = _.map(action.on_failure, function (action) {
var url = 'http://localhost:' + config.viciPort + '/do/' + action.do
return {
url: url,
method: action.method || 'POST',
headers: {
'x-vici-secret': config.viciSecret
},
payload: action.payload || {}
}
})
executors.script({
action: action,
payload: request.payload,
query: request.query,
params: request.params,
callback: function (exitCode) {
// call outHooks here
if (exitCode === 0 && !_.isEmpty(action.on_success)) {
executors.afterExec(on_success)
} else if (!_.isEmpty(action.on_failure)) {
executors.afterExec(on_failure)
}
}
})
return status.ok(reply,
'Webhook successfully triggered ' + action.name)
}
}
})
}
function _isSecretPresentInHeader (request, secret) {
return request.headers['x-vici-secret'] === secret
}
function _isSecretPresentInQuery (request, secret) {
return request.query.secret === secret
}