-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsample_modules.js
More file actions
44 lines (36 loc) · 1.31 KB
/
sample_modules.js
File metadata and controls
44 lines (36 loc) · 1.31 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
'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 } = process.env;
const K = require('no-kafka');
const fs = require('fs' );
fs.writeFileSync('./client.crt', KAFKA_CLIENT_CERT);
fs.writeFileSync('./client.key', KAFKA_CLIENT_CERT_KEY);
const producer = new K.Producer({
clientId: 'sample-module-producer',
connectionString: KAFKA_URL.replace(/\+ssl/g, ''),
ssl: { certFile: './client.crt', keyFile: './client.key', },
});
producer.
init().
then(() => {
for (var msg of sampleGen(5))
producer.send({
topic: KAFKA_TOPIC,
partition: 0,
message: {
value: JSON.stringify(msg),
},
});
});
//TODO in production replace the generator with real http calls
function* sampleGen(i = 5) {
while (--i >= 0)
yield ({
sourceSystemCode: 'loudcloud',
sourceSystemID: randomStringByTemplate('a960f8b0-12ae-4804-b11d-cc8ebd3d60eb'),
});
}
const CHARS_TEMPLPATE = 'qwertyuiopasdfghjklzxcvbnm1234567890';
const CHARS_TEMPLPATE_LENGTH = CHARS_TEMPLPATE.length;
const randomStringByTemplate = (tmpl) => tmpl.replace(/\w/g, () => CHARS_TEMPLPATE.charAt(Math.random() * CHARS_TEMPLPATE_LENGTH));