#!/usr/bin/env node const Monitorcito = require('..'); const Http = require('http'); const { debuglog } = require('util'); const internals = { kUnsetServicesError: 'Please specify comma separated services in MONITORCITO_SERVICES env variable', arguments: null, log: debuglog('monitorcito'), prepareArguments() { internals.log('Validating arguments'); if (!process.env.MONITORCITO_SERVICES) { throw new Error(internals.kUnsetServicesError); } internals.arguments = process.env.MONITORCITO_SERVICES.split(',') internals.log(`Arguments are ${internals.arguments}`); }, startServer() { internals.log('Setting up the server'); const server = Http.createServer(async (request, response) => { internals.log('Incoming request'); const responseBody = JSON.stringify(await Monitorcito(internals.arguments)); internals.log(`Responding with ${responseBody}`); response.writeHead(200, { 'Content-Type': 'application/json' }); response.write(responseBody); response.end(); }); const port = Number(process.env.MONITORCITO_PORT) || 1991; server.listen(port); internals.log(`Listening on port ${port}`); }, async run() { internals.prepareArguments(); internals.startServer(); } }; internals.run() .catch((error) => { console.error(error); process.exit(1); });