-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
37 lines (30 loc) · 983 Bytes
/
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
'use strict'
const fp = require('fastify-plugin')
const supportedIntegrations = {
betterstack: require('./integrations/betterstack')
}
async function fastifyInvincible (fastify, options) {
const {
name = 'fastify-invincible',
pollingUrl,
pollingIntervalSeconds,
integration = 'betterstack'
} = options
const buildIntegrator = supportedIntegrations[integration]
fastify.addHook('onListen', async function hook () {
const instance = buildIntegrator(options[`${integration}Options`])
const result = await instance.createMonitor(name, pollingUrl, pollingIntervalSeconds)
if (result.isCreated === false) {
fastify.log.info(`Monitor already exists at ${result.id}`)
} else {
fastify.log.info(`Monitor created at ${result.id}`)
}
})
}
const plugin = fp(fastifyInvincible, {
name: 'fastify-invincible',
fastify: '^4.x'
})
module.exports = plugin
module.exports.default = plugin
module.exports.fastifyInvincible = plugin