From dfa50ea6b54b60e0dd9cd3258f935f5258b9b275 Mon Sep 17 00:00:00 2001 From: Asif Bacchus Date: Sat, 24 Jul 2021 03:01:01 -0600 Subject: [PATCH] feature: add healthcheck - add express npm to allow creation of health endpoint - create health end pt as callback to livereload server listening state - configure healthcheck in container - allows true 'stack' operation since other services can query status --- build/Dockerfile | 11 +++++++++-- build/livereload.js | 29 ++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/build/Dockerfile b/build/Dockerfile index 9d2c81d..51c5735 100644 --- a/build/Dockerfile +++ b/build/Dockerfile @@ -45,13 +45,13 @@ ENV LR_DEBUG=true ENV LR_HTTPS=true ENV CERT_HOSTNAME="" -# install node-livereload as node user then switch back to root user +# install node-livereload and express as node user then switch back to root user USER node WORKDIR /home/node RUN mkdir -p .npm-global/bin .npm-global/lib \ && npm config set fund false \ && npm config set update-notifier false \ - && npm install livereload --save + && npm install livereload express --save # copy scripts and fix-up all permissions USER root @@ -63,6 +63,13 @@ RUN chown node:node /home/node/livereload.js \ && chmod 755 /usr/local/bin/entrypoint.sh \ && chmod 644 /etc/selfsigned.cnf +HEALTHCHECK \ + --interval=10s \ + --timeout=5s \ + --start-period=60s \ + --retries=3 \ + CMD wget --spider -T 3 -q localhost:3000/api/v1/health || exit 1 + # switch to node user, run entrypoint script by default USER node WORKDIR /home/node diff --git a/build/livereload.js b/build/livereload.js index 571c460..6c6514c 100644 --- a/build/livereload.js +++ b/build/livereload.js @@ -1,7 +1,30 @@ // implement node-livereload over an HTTPS connection +// healthcheck function +function healthcheck() { + const express = require('express'); + const http = require('http'); + + const app = express(); + const router = express.Router(); + + router.use((req, res, next) =>{ + res.header('Access-Control-Allow-Methods', 'GET'); + next(); + }); + + router.get('/health', (req, res) =>{ + res.status(200).send('Ok'); + }); + + app.use('/api/v1', router); + + const hServer = http.createServer(app); + hServer.listen(3000); +} + // load livereload module -let livereload = require('livereload'); +const livereload = require('livereload'); // set createServer options const fs = require('fs'); @@ -32,7 +55,7 @@ else { } // start server -let server = livereload.createServer(options); -server.watch('/watch') +const lrServer = livereload.createServer(options, healthcheck); +lrServer.watch('/watch') //#EOF