]>
Commit | Line | Data |
---|---|---|
2d72aab0 RBR |
1 | #!/usr/bin/env node |
2 | ||
3 | const Monitorcito = require('..'); | |
4 | const Http = require('http'); | |
5 | const { debuglog } = require('util'); | |
6 | ||
7 | const internals = { | |
8 | ||
9 | kUnsetServicesError: 'Please specify comma separated services in MONITORCITO_SERVICES env variable', | |
10 | ||
11 | arguments: null, | |
12 | log: debuglog('monitorcito'), | |
13 | ||
14 | prepareArguments() { | |
15 | ||
16 | internals.log('Validating arguments'); | |
17 | if (!process.env.MONITORCITO_SERVICES) { | |
18 | throw new Error(internals.kUnsetServicesError); | |
19 | } | |
20 | ||
21 | internals.arguments = process.env.MONITORCITO_SERVICES.split(',') | |
22 | internals.log(`Arguments are ${internals.arguments}`); | |
23 | }, | |
24 | ||
25 | startServer() { | |
26 | ||
27 | internals.log('Setting up the server'); | |
28 | const server = Http.createServer(async (request, response) => { | |
29 | ||
30 | internals.log('Incoming request'); | |
31 | const responseBody = JSON.stringify(await Monitorcito(internals.arguments)); | |
32 | internals.log(`Responding with ${responseBody}`); | |
33 | ||
34 | response.writeHead(200, { 'Content-Type': 'application/json' }); | |
35 | response.write(responseBody); | |
36 | response.end(); | |
37 | }); | |
38 | const port = Number(process.env.MONITORCITO_PORT) || 1991; | |
39 | server.listen(port); | |
40 | internals.log(`Listening on port ${port}`); | |
41 | }, | |
42 | ||
43 | async run() { | |
44 | ||
45 | internals.prepareArguments(); | |
46 | internals.startServer(); | |
47 | } | |
48 | }; | |
49 | ||
50 | internals.run() | |
51 | .catch((error) => { | |
52 | ||
53 | console.error(error); | |
54 | process.exit(1); | |
55 | }); |