Repository: OWASP/threat-dragon
Pull request #474 (2022-08-07 07:34:30)

expand healthz to show uptime
**Summary**
Expanded healthz, with a bit of re-arranging, to show html in its response

**Description for the changelog**
expand healthz to show uptime

**Other info**




Patch:
diff --git a/docs/development/env.md b/docs/development/env.md
index 59318eb9..65e7bc90 100644
--- a/docs/development/env.md
+++ b/docs/development/env.md
@@ -170,7 +170,7 @@ ___
 | LOG_MAX_FILE_SIZE | Maximum size of the back-end express server log file, in MB | `24` |
 | NODE_ENV | The node environment, typically `test`, `production` or `development`.  The 'secure' cookie flag is set only if running in `production` mode | |
 | PORT | Defines the listening port for Threat Dragon's server, and used by Heroku | `3000`|
-| SERVER_API_PROTOCOL | The protocol used between Threat Dragon's server and frontend, used by Heroku (http/https) | `http` |
+| SERVER_API_PROTOCOL | The protocol used between Threat Dragon's server and frontend, used by Heroku: `http` / `https` | `https` |
 
 {:.table .table-striped}
 | Back-end specific variables | Description | Default |
diff --git a/docs/development/internationalization.md b/docs/development/internationalization.md
index 7c3f63c1..a740162a 100644
--- a/docs/development/internationalization.md
+++ b/docs/development/internationalization.md
@@ -8,7 +8,9 @@ group: Internationalization
 
 ## [OWASP](https://www.owasp.org) Threat Dragon
 
-Internationalization is enabled by [vue-i18n](https://kazupon.github.io/vue-i18n/).  At the time of writing, there are not any translations available.  Hopefully this feature enables our community members to translate the application to new languages so it can be used by more people.
+Internationalization is enabled by [vue-i18n](https://kazupon.github.io/vue-i18n/).
+At the time of writing, there are not any translations available.
+Hopefully this feature enables our community members to translate the application to new languages so it can be used by more people.
 
 To add a new language:
 - Add a new file with the locale as the name (eg "de" or "en") in the `src/i18n` directory
@@ -16,4 +18,4 @@ To add a new language:
 - Import your file to `src/i18n/index.js` and add it to the `messages` object in the `get` method
 - Choose your new locale from the dropdown to test your translations
 
-By default, if a translation key cannot be found, it will fall-back to the default locale (`en`)
\ No newline at end of file
+By default, if a translation key cannot be found, it will fall-back to the default locale (`en`)
diff --git a/td.server/src/config/routes.config.js b/td.server/src/config/routes.config.js
index 8925930a..36e70f6d 100644
--- a/td.server/src/config/routes.config.js
+++ b/td.server/src/config/routes.config.js
@@ -2,7 +2,7 @@
 
 import auth from '../controllers/auth.js';
 import bearer from './bearer.config.js';
-import healthcheck from '../controllers/healthcheck.js';
+import healthcheck from '../controllers/healthz.js';
 import homeController from '../controllers/homecontroller.js';
 import threatmodelController from '../controllers/threatmodelcontroller.js';
 
diff --git a/td.server/src/controllers/healthcheck.js b/td.server/src/controllers/healthcheck.js
deleted file mode 100644
index 2b55c58c..00000000
--- a/td.server/src/controllers/healthcheck.js
+++ /dev/null
@@ -1,12 +0,0 @@
-import loggerHelper from '../helpers/logger.helper.js';
-
-const logger = loggerHelper.get('controllers/healthcheck.js');
-
-const healthz = (req, res) => {
-    logger.debug('API healthz request: ' + req);
-    return res.send(true);
-};
-
-export default {
-    healthz
-};
diff --git a/td.server/src/controllers/healthz.js b/td.server/src/controllers/healthz.js
new file mode 100644
index 00000000..24fe1af1
--- /dev/null
+++ b/td.server/src/controllers/healthz.js
@@ -0,0 +1,15 @@
+// health-check endpoint for applications
+
+const healthz = (req, res) => {
+    const data = {
+        uptime: process.uptime(),
+        message: 'OK',
+        date: new Date()
+    };
+
+    return res.status(200).send(data);
+};
+
+export default {
+    healthz
+};
diff --git a/td.server/src/healthcheck.js b/td.server/src/healthcheck.js
index 5420a397..f17b81c8 100644
--- a/td.server/src/healthcheck.js
+++ b/td.server/src/healthcheck.js
@@ -1,16 +1,22 @@
-/* eslint-disable no-console */
+import env from 'env/Env.js';
+import loggerHelper from 'helpers/logger.helper.js';
+
+const logger = loggerHelper.get('healthcheck.js');
 const http = require('http');
 
-http.get('http://localhost:3000/healthz', (res) => {
+const req = (env.get().config.SERVER_API_PROTOCOL || 'https') + '://localhost:' + (env.get().config.PORT || '3000') + '/healthz';
+
+http.get(req, (res) => {
     const { statusCode } = res;
+    logger.debug('Health check request: ' + req);
     
     if (statusCode !== 200) {
-        console.error(`Healthcheck failure: invalid status code: ${statusCode}`);
+        logger.error(`Healthcheck failure: invalid status code: ${statusCode}`);
         process.exit(1);
     }
 
     process.exit(0);
 }).on('error', (e) => {
-    console.error(`Healthcheck failure: ${e.message}`);
+    logger.error(`Healthcheck failure: ${e.message}`);
     process.exit(1);
 });


Test patch:
diff --git a/td.server/test/config/routes.config.spec.js b/td.server/test/config/routes.config.spec.js
index a1bad931..642938b5 100644
--- a/td.server/test/config/routes.config.spec.js
+++ b/td.server/test/config/routes.config.spec.js
@@ -4,7 +4,7 @@ import sinon from 'sinon';
 
 import auth from '../../src/controllers/auth.js';
 import bearer from '../../src/config/bearer.config.js';
-import healthcheck from '../../src/controllers/healthcheck.js';
+import healthcheck from '../../src/controllers/healthz.js';
 import homeController from '../../src/controllers/homecontroller.js';
 import { getMockApp } from '../mocks/express.mocks.js';
 import routeConfig from '../../src/config/routes.config.js';
diff --git a/td.server/test/controllers/healthcheck.spec.js b/td.server/test/controllers/healthcheck.spec.js
index 1aaffa93..dea2b354 100644
--- a/td.server/test/controllers/healthcheck.spec.js
+++ b/td.server/test/controllers/healthcheck.spec.js
@@ -2,9 +2,9 @@ import { expect } from 'chai';
 import sinon from 'sinon';
 
 import { getMockResponse } from '../mocks/express.mocks.js';
-import healthcheck from '../../src/controllers/healthcheck.js';
+import healthcheck from '../../src/controllers/healthz.js';
 
-describe('controllers/healthcheck.js', () => {
+describe('controllers/healthz.js', () => {
     let mockResponse;
 
     beforeEach(() => {
@@ -13,6 +13,6 @@ describe('controllers/healthcheck.js', () => {
     });
 
     it('sends a true response', () => {
-        expect(mockResponse.send).to.have.been.calledWith(true);
+        expect(mockResponse.status).to.have.been.calledWith(200);
     });
 });