-
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.
first implementation for Koa support
- Loading branch information
Showing
8 changed files
with
103 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,23 @@ | ||
const Koa = require('koa'); | ||
const app = new Koa(); | ||
const healthcheck = require("../"); | ||
|
||
app.use(koaMiddleWare); | ||
|
||
// response | ||
app.use(ctx => { | ||
ctx.body = 'Hello Koa'; | ||
const check = healthcheck(); | ||
check.addCheck('cassandra', 'timeout', async() => { | ||
return { | ||
status : 'pass', | ||
bullshit : false, | ||
metricValue: 250, | ||
"metricUnit": "ms" | ||
}; | ||
}); | ||
|
||
app.listen(3535); | ||
app.use(check.koa()); | ||
|
||
const check2 = healthcheck({"path" : "/ping"}); | ||
app.use(check2.koa()); | ||
|
||
app.listen(3535, () => console.log('Example app listening on port 3535!')); | ||
|
||
//app.listen(3535); | ||
|
||
async function koaMiddleWare(ctx, next) { | ||
if (ctx.request.originalUrl != '/health') return next(); | ||
ctx.set('Content-Type', `application/json`); | ||
ctx.response.status = 201; | ||
ctx.body= {"name" : "response"}; | ||
} |
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
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,61 @@ | ||
const test = require('blue-tape'); | ||
const nf = require('node-fetch'); | ||
|
||
test('Basic Healthy Koa Health Check', async t => { | ||
|
||
// avoid problems if this env var is already set from wherever test was run | ||
process.env.NODE_HEALTH_ENDPOINT_PATH = ""; | ||
|
||
const server = getServer(); | ||
const util = require('../support/util'); | ||
const baseuri = util.serverUri(server); | ||
|
||
try { | ||
const res = await nf(`${baseuri}/hello`); | ||
|
||
const res2 = await nf(`${baseuri}/health`); | ||
t.equal(res2.status, 200, 'proper http status code for /health'); | ||
t.equal(res2.headers.get('content-type'), | ||
'application/health+json; charset=utf-8', | ||
'proper content type for health endpoint'); | ||
const response = await res2.json(); | ||
t.same(response.status, 'pass', | ||
'healthcheck endpoint status works'); | ||
//t.same(response.details["cassandra:timeout"].metricUnit, 'ms', | ||
// 'healthcheck endpoint details work'); | ||
|
||
} catch (err) { | ||
t.fail(err); | ||
} | ||
|
||
server.close(); | ||
|
||
}); | ||
|
||
function getServer() { | ||
const http = require('http'); | ||
const Koa = require('koa'); | ||
const app = new Koa(); | ||
|
||
const healthcheck = require('../../lib/health')(); | ||
|
||
healthcheck.addCheck('backend', 'koa-downstream', async() => { | ||
return { | ||
status : 'pass', | ||
metricValue: 17, | ||
metricUnit: "units" | ||
}; | ||
}); | ||
|
||
app.use(healthcheck.koa()); | ||
|
||
const server = http.createServer(app.callback()); | ||
|
||
server.listen(0, function(err) { | ||
if (err) console.error(err) && process.exit(1); | ||
const port = server.address().port; | ||
//console.log(`Test server listening at port ${port} \n`); | ||
}); | ||
|
||
return server; | ||
} |
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