-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathindex.js
33 lines (30 loc) · 892 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
/*
* Copyright 2024 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
'use strict'
const fastify = require('fastify')({ logger: true })
const { Kafka } = require('kafkajs')
const kafka = new Kafka({
clientId: 'example-producer',
brokers: ['localhost:9093']
})
const producer = kafka.producer()
const { PORT: port = 3000, HOST: host = '127.0.0.1' } = process.env
fastify.listen({ host, port }, function listener(err) {
if (err) {
fastify.log.error(err)
// eslint-disable-next-line no-process-exit
process.exit(1)
}
})
fastify.post('/message', async (request, reply) => {
const { topic = 'test-topic', messages = [{ key: 'key', value: 'test-message' }] } =
request.body || {}
await producer.connect()
await producer.send({
topic,
messages
})
return reply.send(`Sent ${messages.length} messages to ${topic}`)
})