aboutsummaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorRuben Beltran del Rio <ruben@unlimited.pizza>2021-06-08 21:37:55 +0200
committerRuben Beltran del Rio <ruben@unlimited.pizza>2021-06-08 21:37:55 +0200
commit2d72aab0a88baa906127489e1c25fd11881bdc35 (patch)
tree0f213b96c74fc6465e4ea07123a39f8dc469c857 /bin
Add the monitor
Diffstat (limited to 'bin')
-rwxr-xr-xbin/monitorcito55
1 files changed, 55 insertions, 0 deletions
diff --git a/bin/monitorcito b/bin/monitorcito
new file mode 100755
index 0000000..2d41152
--- /dev/null
+++ b/bin/monitorcito
@@ -0,0 +1,55 @@
+#!/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);
+ });