-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrest_publisher.js
More file actions
55 lines (42 loc) · 1.33 KB
/
rest_publisher.js
File metadata and controls
55 lines (42 loc) · 1.33 KB
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
'use strict';
if ([ 'KAFKA_TOPIC', 'KAFKA_CLIENT_CERT', 'KAFKA_CLIENT_CERT_KEY', 'KAFKA_URL', ].some( (key) => !(key in process.env) ))
throw new Error(`Missing enviroment value!`);
const { KAFKA_TOPIC, KAFKA_CLIENT_CERT, KAFKA_CLIENT_CERT_KEY, KAFKA_URL, PORT = 5000 } = process.env;
const kafka = require('no-kafka');
const express = require('express');
const fs = require('fs' );
fs.writeFileSync('./client.crt', KAFKA_CLIENT_CERT);
fs.writeFileSync('./client.key', KAFKA_CLIENT_CERT_KEY);
const producer = new kafka.Producer({
clientId: 'sample-module-producer',
connectionString: KAFKA_URL.replace(/\+ssl/g, ''),
ssl: { certFile: './client.crt', keyFile: './client.key', },
});
producer.init();
const send = msg => producer.send({
topic: KAFKA_TOPIC,
partition: 0,
message: {
value: JSON.stringify({
sourceSystemCode: 'loudcloud',
sourceSystemID: msg.Id,
}),
},
})
const app = express();
app.
set('port', PORT).
use(require('body-parser').json()).
get('/', ((req, res) => res.send(
'REST Endpoints:\n' +
'/module/loudcloud\n'
))).
post('/module/loudcloud', ((req, res) => {
if (Array.isArray(req.body)) {
for (var m of req.body)
send(m);
} else
send(req.body);
res.sendStatus(200);
})).
listen(PORT, (() => console.log.bind(`Node app is running on port ${ PORT }`)));