2021-07-30 17:06:05 -06:00
|
|
|
/*
|
|
|
|
Implement node-livereload over HTTP or HTTPS connection with integrated
|
|
|
|
health-check, signal handling and graceful socket shutdown.
|
|
|
|
*/
|
2021-07-22 16:43:17 -06:00
|
|
|
|
2021-07-30 17:06:05 -06:00
|
|
|
// load required modules
|
2021-07-24 03:01:01 -06:00
|
|
|
const livereload = require('livereload');
|
2021-07-28 03:51:56 -06:00
|
|
|
const fs = require('fs');
|
2021-07-30 17:06:05 -06:00
|
|
|
const http = require('http');
|
|
|
|
|
|
|
|
// health check object
|
|
|
|
const healthCheck = {
|
|
|
|
app: healthCheckApp(),
|
|
|
|
server: function () {
|
|
|
|
return http.createServer(this.app);
|
|
|
|
},
|
|
|
|
start: function () {
|
|
|
|
this.server().listen(3000);
|
|
|
|
},
|
|
|
|
stop: function (callback) {
|
|
|
|
this.server().close(callback());
|
|
|
|
}
|
|
|
|
};
|
2021-07-28 03:51:56 -06:00
|
|
|
|
2021-07-30 17:06:05 -06:00
|
|
|
// set LiveReload server options
|
2021-07-28 03:51:56 -06:00
|
|
|
const extraExclusions = process.env.LR_EXCLUDE.split(",");
|
|
|
|
extraExclusions.forEach((exclusion, idx) => {
|
|
|
|
extraExclusions[idx] = new RegExp(exclusion);
|
|
|
|
});
|
2021-07-22 16:43:17 -06:00
|
|
|
|
2021-07-30 17:06:05 -06:00
|
|
|
// noinspection SpellCheckingInspection
|
|
|
|
const lrOptions = {
|
2021-07-22 16:43:17 -06:00
|
|
|
port: process.env.LR_PORT,
|
|
|
|
exts: process.env.LR_EXTS,
|
2021-07-28 03:51:56 -06:00
|
|
|
exclusions: extraExclusions,
|
2021-07-22 16:43:17 -06:00
|
|
|
usePolling: true,
|
2021-07-28 04:12:19 -06:00
|
|
|
delay: process.env.LR_DELAY
|
2021-07-22 16:43:17 -06:00
|
|
|
};
|
2021-07-30 17:06:05 -06:00
|
|
|
if (process.env.LR_DEBUG === 'true') {
|
|
|
|
lrOptions.debug = true;
|
2021-07-23 23:45:58 -06:00
|
|
|
console.log("[Debug output ENABLED]");
|
2021-07-23 23:04:44 -06:00
|
|
|
}
|
2021-07-30 17:06:05 -06:00
|
|
|
if (process.env.LR_HTTPS === 'true') {
|
|
|
|
lrOptions.https = {
|
2021-07-23 13:31:01 -06:00
|
|
|
cert: fs.readFileSync('/certs/fullchain.pem'),
|
|
|
|
key: fs.readFileSync('/certs/privkey.pem')
|
|
|
|
};
|
2021-07-23 23:45:58 -06:00
|
|
|
console.log("[HTTPS mode]");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.log("[HTTP mode]");
|
2021-07-23 13:31:01 -06:00
|
|
|
}
|
|
|
|
|
2021-07-30 17:06:05 -06:00
|
|
|
// start LiveReload server
|
|
|
|
// noinspection JSVoidFunctionReturnValueUsed
|
|
|
|
const lrServer = livereload.createServer(lrOptions, healthCheck.start());
|
|
|
|
lrServer.watch('/watch');
|
2021-07-22 16:43:17 -06:00
|
|
|
|
2021-07-30 17:06:05 -06:00
|
|
|
// graceful termination on signals
|
|
|
|
const termSignals = {
|
|
|
|
'SIGHUP': 1,
|
|
|
|
'SIGINT': 2,
|
|
|
|
'SIGTERM': 15
|
|
|
|
};
|
|
|
|
const shutdown = async (signal, value) => {
|
|
|
|
console.log("shutting down...\n");
|
|
|
|
await lrServer.close();
|
|
|
|
await healthCheck.stop(() => {
|
|
|
|
console.log("health-check STOPPED\n");
|
|
|
|
});
|
|
|
|
console.log(`server stopped after receiving ${signal}(${value}).`);
|
|
|
|
}
|
|
|
|
// iterate signals and create listener for each
|
|
|
|
Object.keys(termSignals).forEach((signal) => {
|
|
|
|
process.on(signal, () => {
|
|
|
|
console.log("\n! received signal:", signal, " !");
|
|
|
|
shutdown(signal, termSignals[signal]).then(() => {
|
|
|
|
process.exit(128 + termSignals[signal]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// functions
|
|
|
|
|
|
|
|
function healthCheckApp() {
|
|
|
|
const express = require('express');
|
|
|
|
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');
|
|
|
|
});
|
|
|
|
return app.use('/api/v1', router);
|
|
|
|
}
|