-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
50 lines (44 loc) · 885 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
38
39
40
41
42
43
44
45
46
47
48
49
50
'use strict'
const fp = require('fastify-plugin')
const amqpClient = require('amqplib')
function getTarget ({
frameMax,
heartbeat,
hostname,
locale,
password,
port,
url,
username,
vhost
}) {
if (url) {
return url
} else if (hostname) {
return {
frameMax,
heartbeat,
hostname,
locale,
password,
port,
username,
vhost
}
} else {
throw new Error('`url` parameter is mandatory if no hostname is provided')
}
}
async function fastifyAmqp (fastify, options) {
const connection = await amqpClient.connect(getTarget(options), options.socket)
fastify.addHook('onClose', () => connection.close())
const channel = await connection.createChannel()
fastify.decorate('amqp', {
connection,
channel
})
}
module.exports = fp(fastifyAmqp, {
fastify: '>=1.0.0',
name: 'fastify-amqp'
})