Skip to content

Commit

Permalink
Add new /health/info endpoint (#2225)
Browse files Browse the repository at this point in the history
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
Cruikshanks authored Oct 25, 2022
1 parent 945f529 commit 70e5436
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/external/modules/routes.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
'use strict'

// Internal only routes
const addLicencesRoutes = require('./add-licences/routes')
const coreRoutes = require('./core/routes')
const contentRoutes = require('./content/routes')
Expand All @@ -10,6 +13,9 @@ const accountRoutes = require('./account/routes')
const notificationRoutes = require('./notifications/routes')
const notifyRoutes = require('./notify/routes')

// Shared routes
const healthRoutes = require('../../shared/modules/health/routes')

module.exports = [
...Object.values(addLicencesRoutes),
...Object.values(coreRoutes),
Expand All @@ -21,5 +27,6 @@ module.exports = [
...Object.values(companySelector),
...Object.values(notificationRoutes),
...Object.values(accountRoutes),
...Object.values(notifyRoutes)
...Object.values(notifyRoutes),
...Object.values(healthRoutes)
]
11 changes: 9 additions & 2 deletions src/internal/modules/routes.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
'use strict'

// External only routes
const coreRoutes = require('./core/routes')
const contentRoutes = require('./content/routes')
const notificationsRoutes = require('./notifications/routes')
Expand All @@ -18,12 +21,15 @@ const chargeInformationRoutes = require('./charge-information/routes')
const chargeInformationUploadRoutes = require('./charge-information-upload/routes')
const agreementsRoutes = require('./agreements/routes')
const reportingRoutes = require('./reporting/routes')
const kpiReporting = require('../../internal/modules/kpi-reporting/routes')
const viewLicences = require('./view-licences/routes')
const gaugingStations = require('./gauging-stations/routes')
const customers = require('./customers/routes')
const notes = require('./notes/routes')

// Shared routes
const healthRoutes = require('../../shared/modules/health/routes')
const kpiReporting = require('../../internal/modules/kpi-reporting/routes')

module.exports = [
...Object.values(coreRoutes),
...Object.values(contentRoutes),
Expand All @@ -50,5 +56,6 @@ module.exports = [
...Object.values(viewLicences),
...Object.values(gaugingStations),
...Object.values(customers),
...Object.values(notes)
...Object.values(notes),
...Object.values(healthRoutes)
]
25 changes: 25 additions & 0 deletions src/shared/modules/health/controller.js
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
14 changes: 14 additions & 0 deletions src/shared/modules/health/routes.js
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
}
}
}
29 changes: 29 additions & 0 deletions test/shared/modules/health/controller.test.js
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()
})
})
})

0 comments on commit 70e5436

Please sign in to comment.