-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (62 loc) · 1.46 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const VERSION = require('./package.json').version;
const fs = require('fs');
const Https = require('https');
const Http = require('http');
const handle_request = require('./lib/handle_request');
let publisher = null;
async function initOutputStream() {
publisher = require('./lib/send_results_es');
await publisher.init()
handle_request.init(publisher);
}
function initServer() {
let key;
let cert;
if (process.env.KEY_PATH) {
key = fs.readFileSync(process.env.KEY_PATH);
} else {
key = null;
}
if (process.env.CERT_PATH) {
cert = fs.readFileSync(process.env.CERT_PATH);
} else {
cert = null;
}
const PORT = process.env.PORT || 9205;
const serverOptions = {
config: {
port: PORT
}
}
let useSSL = false;
if (key && cert) {
useSSL = true;
serverOptions.config.key = key;
serverOptions.config.cert = cert;
}
let server;
if (useSSL) {
server = Https.createServer(serverOptions.config, handle_request.handle);
} else {
server = Http.createServer(handle_request.handle);
}
console.log(`Starting Elastic Query Tracker v${VERSION} on port ${PORT}`);
server.listen(serverOptions.config.port);
}
//Let's setup the publisher
async function init() {
try {
await initOutputStream();
}
catch (ex) {
console.error('Failed to init output stream.', ex);
}
//Let's start listening
try {
initServer();
}
catch (ex) {
console.error('Failed to start service.', ex)
}
}
init();