-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (50 loc) · 1.99 KB
/
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
51
52
53
54
55
56
57
const net = require('net');
//Make options available globally
const { options } = require('./argparser.js');
global.options = options
const { Sinks } = require('./sinks.js');
const { Source } = require('./source.js');
const { DataGenerator } = require('./generator.js');
const { createAPIMocker, createConfigManagerAPI, createSourceAPI, createSinkAPI } = require('./api.js')
const { Distributor } = require('./distributor.js');
const { logger } = require('./logger')
const { SchemaManager} = require('./schema_manager')
// Load generator environment.
// Read here - https://github.com/motdotla/dotenv
//require('dotenv').config()
logger.info(options)
// This is implemented as singleton, so create it once and making it available globally.
// Avoids having to create module dependencies.
global.schemaManager = SchemaManager.getInstance()
// Generator is singleton, so can be set globally. This avoids having to include generator module everywhere
// just to get this singleton instance
global.generator = DataGenerator.getInstance();
// Create generators for sources
Source.getInstance();
Distributor.loadSinks()
createAPIMocker()
createConfigManagerAPI()
createSinkAPI()
createSourceAPI()
// Create data server for console sink
const server = net.createServer({ allowHalfOpen: true }, (c) => {
let socketAddress = c.address();
let consoleSink = new Sinks.console(c);
logger.info('Socket client connected');
Distributor.addSink("console_default",consoleSink);
c.on('end', () => {
logger.info('Socket client disconnected');
// On dis-connection, detach fake record generator
Distributor.deleteSinkWithInstance(consoleSink)
});
c.on('error', () => {
logger.info("Tried writing to client," + JSON.stringify(socketAddress) + ", which disconnected");
});
});
server.on('error', (err) => {
throw err;
});
// Data Server: Use the port from arguments or use default
server.listen(options.port || process.env.PORT || 4000, () => {
logger.info('server bound');
});