Skip to content

Commit

Permalink
feat(health-check): extend info response with the resolved health-che…
Browse files Browse the repository at this point in the history
…ck promise
  • Loading branch information
tnguyen14 authored and gergelyke committed Jul 8, 2018
1 parent 0f5a518 commit b79949d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ function onShutdown () {
console.log('cleanup finished, server is shutting down');
}

function healthCheck () {
return Promise.resolve(
// optionally include a resolve value to be included as
// info in the healthcheck response
)
}

const server = http.createServer((request, response) => {
response.end(
`<html>
Expand All @@ -47,7 +54,7 @@ const server = http.createServer((request, response) => {
const options = {
// healtcheck options
healthChecks: {
'/healthcheck': check // a promise returning function indicating service health
'/healthcheck': healthCheck // a promise returning function indicating service health
},

// cleanup options
Expand Down
12 changes: 9 additions & 3 deletions lib/terminus.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ function noopResolves () {
return Promise.resolve()
}

function sendSuccess (res) {
function sendSuccess (res, info) {
res.statusCode = 200
if (info) {
return res.end(JSON.stringify({
status: 'ok',
info: info
}))
}
res.end(SUCCESS_RESPONSE)
}

Expand All @@ -42,8 +48,8 @@ function decorateWithHealthCheck (server, options) {
return sendFailure(res)
}
healthChecks[req.url]()
.then(() => {
sendSuccess(res)
.then((info) => {
sendSuccess(res, info)
})
.catch((error) => {
logger('healthcheck failed', error)
Expand Down
33 changes: 33 additions & 0 deletions lib/terminus.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,39 @@ describe('Terminus', () => {
.catch(done)
})

it('includes info on resolve', (done) => {
let onHealthCheckRan = false

terminus(server, {
healthChecks: {
'/health': () => {
onHealthCheckRan = true
return Promise.resolve({
version: '1.0.0'
})
}
}
})
server.listen(8000)

fetch('http://localhost:8000/health')
.then(res => {
expect(res.status).to.eql(200)
expect(onHealthCheckRan).to.eql(true)
return res.json()
})
.then(json => {
expect(json).to.deep.eql({
status: 'ok',
info: {
version: '1.0.0'
}
})
done()
})
.catch(done)
})

it('returns 503 on reject', (done) => {
let onHealthCheckRan = false
let loggerRan = false
Expand Down

0 comments on commit b79949d

Please sign in to comment.