-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new /health/info endpoint (#2225)
DEFRA/water-abstraction-team#54 This supports the new service-status page we're building in the [water-abstraction-system repo](https://github.com/DEFRA/water-abstraction-system). We want to be able to show both the version and commit hash for each service on that page. The existing `/status` page does include the `package.json` version. But that endpoint is also used for the AWS load balancer health check so is heavily hit. We don't want to make it do more work getting the commit hash. Instead, we add a new `/health/` path and a new `/info` endpoint to return both the version and commit hash. ** Notes - We want the endpoint available in both internal and external apps. So, we add the functionality to the shared folder
- Loading branch information
1 parent
945f529
commit 70e5436
Showing
5 changed files
with
85 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict' | ||
|
||
// We use promisify to wrap exec in a promise. This allows us to await it without resorting to using callbacks. | ||
const util = require('util') | ||
const exec = util.promisify(require('child_process').exec) | ||
|
||
const pkg = require('../../../../package.json') | ||
|
||
const _getCommitHash = async () => { | ||
try { | ||
const { stdout, stderr } = await exec('git rev-parse HEAD') | ||
return stderr ? `ERROR: ${stderr}` : stdout.replace('\n', '') | ||
} catch (error) { | ||
return `ERROR: ${error.message}` | ||
} | ||
} | ||
|
||
const getInfo = async () => { | ||
return { | ||
version: pkg.version, | ||
commit: await _getCommitHash() | ||
} | ||
} | ||
|
||
exports.getInfo = getInfo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict' | ||
|
||
const controller = require('./controller') | ||
|
||
module.exports = { | ||
getInfo: { | ||
method: 'GET', | ||
path: '/health/info', | ||
handler: controller.getInfo, | ||
config: { | ||
auth: false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict' | ||
|
||
// Test framework dependencies | ||
const { test, experiment, before } = exports.lab = require('@hapi/lab').script() | ||
const { expect } = require('@hapi/code') | ||
|
||
// Test helpers | ||
const pkg = require('../../../../package.json') | ||
|
||
// Thing under test | ||
const controller = require('../../../../src/shared/modules/health/controller') | ||
|
||
experiment('modules/health/controller', () => { | ||
experiment('.getInfo', () => { | ||
let info | ||
|
||
before(async () => { | ||
info = await controller.getInfo() | ||
}) | ||
|
||
test('contains the expected water service version', async () => { | ||
expect(info.version).to.equal(pkg.version) | ||
}) | ||
|
||
test('contains the git commit hash', async () => { | ||
expect(info.commit).to.exist() | ||
}) | ||
}) | ||
}) |