-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
49 lines (41 loc) · 1.48 KB
/
server.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
'use strict'
const path = require('path')
const domapic = require('domapic-service')
const requestPromise = require('request-promise')
const options = require('./lib/options')
const { WEBHOOK_URI, WEBHOOK_METHOD } = require('./lib/statics')
const pluginConfigs = require('./lib/plugins')
const ABILITY = 'webhook'
domapic.createModule({
packagePath: path.resolve(__dirname),
customConfig: options
}).then(async dmpcModule => {
const config = await dmpcModule.config.get()
await dmpcModule.register({
[ABILITY]: {
description: 'Launch a request to a webhook',
event: {
description: 'The webhook has been triggered'
},
action: {
description: 'Trigger the webhook',
handler: async () => {
await dmpcModule.tracer.info(`Sending ${config[WEBHOOK_METHOD]} request to ${config[WEBHOOK_URI]}`)
return requestPromise({
uri: config[WEBHOOK_URI],
method: config[WEBHOOK_METHOD]
}).then(() => {
dmpcModule.events.emit(ABILITY)
return Promise.resolve()
}).catch(async error => {
const errorMessage = `Error ${error.response.statusCode} received from ${config[WEBHOOK_METHOD]} to ${config[WEBHOOK_URI]}`
await dmpcModule.tracer.error(errorMessage)
return Promise.reject(new dmpcModule.errors.BadGateway(errorMessage))
})
}
}
}
})
await dmpcModule.addPluginConfig(pluginConfigs)
return dmpcModule.start()
})