Skip to content

Commit

Permalink
first implementation for Koa support
Browse files Browse the repository at this point in the history
  • Loading branch information
inadarei committed Aug 7, 2018
1 parent b51b089 commit a98f5d6
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 18 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defined in the [healthcheck draft RFC](https://tools.ietf.org/html/draft-inadare
### Immediate Priority

- [x] Express/Connect
- [ ] Koa
- [x] Koa
- [ ] Pure Node, no frameworks

### Open to Community Contributions
Expand All @@ -43,13 +43,16 @@ defined in the [healthcheck draft RFC](https://tools.ietf.org/html/draft-inadare

## Usage

### Examples for Express.js

Basic Usage:

```javascript
const healthcheck = require('maikai');

// Add middleware to your Express app:
app.use(healthcheck().express());
app.listen(3535);
```

Advanced usage with custom health checker:
Expand All @@ -73,6 +76,20 @@ check.addCheck('cassandra', 'timeout', async () => {

// Add middleware to your Express app:
app.use(check.express());
app.listen(3535);
```

### Example for Koa.js

```javascript
const Koa = require('koa');
const app = new Koa();
const healthcheck = require("../");

const check = healthcheck();

app.use(check.koa());
app.listen(3535);
```

## Kubernetes Liveness and Readiness Probes
Expand Down
29 changes: 17 additions & 12 deletions examples/serverKoa.js
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"};
}
4 changes: 3 additions & 1 deletion lib/health.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ class HealthCheck {
const response = await this.healthResponse(this.opts);
const httpCode = (response.status === STATUS_ERR ) ? 503 : 200;

ctx.set('Content-Type', 'application/health+json');

ctx.response.status = httpCode;
ctx.body= response;
// Make sure this comes after ctx.body : https://github.com/koajs/koa/issues/1120
ctx.set('Content-Type', 'application/health+json; charset=utf-8');
};
}

Expand Down
2 changes: 1 addition & 1 deletion test/acceptance/express-basic-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const test = require('blue-tape');
const nf = require('node-fetch');
const log = require('metalogger')();

test('Basic Healthy Health Check', async t => {
test('Basic Healthy Express Health Check', async t => {

// avoid problems if this env var is already set from wherever test was run
process.env.NODE_HEALTH_ENDPOINT_PATH = "";
Expand Down
2 changes: 1 addition & 1 deletion test/acceptance/express-failing-check-basic-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const test = require('blue-tape');
const nf = require('node-fetch');
const log = require('metalogger')();

test('Failing Health Check', async t => {
test('Failing Express Health Check', async t => {
const brokenserver = require('../support/failing-check-server').server;
try {
const util = require('../support/util');
Expand Down
2 changes: 1 addition & 1 deletion test/acceptance/express-unknown-response-status-test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const test = require('blue-tape');
const nf = require('node-fetch');

test('Unknown Response Status From a Dependency', async t => {
test('Unknown Express Response Status From a Dependency', async t => {
const server = getServer();
try {
const util = require('../support/util');
Expand Down
61 changes: 61 additions & 0 deletions test/acceptance/koa-basic-test.js
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;
}
2 changes: 1 addition & 1 deletion test/acceptance/path-override-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const test = require('blue-tape');
const nf = require('node-fetch');
const log = require('metalogger')();

test('Overriding health endpoint URI', async t => {
test('Overriding Express health endpoint URI', async t => {

const OVERRIDDEN_PATH = "/ping";
process.env.NODE_HEALTH_ENDPOINT_PATH = OVERRIDDEN_PATH;
Expand Down

0 comments on commit a98f5d6

Please sign in to comment.