Skip to content

Commit

Permalink
Merge pull request #28 from AugustHome/fastify-support
Browse files Browse the repository at this point in the history
Add Fastify support
  • Loading branch information
irnnr committed Apr 11, 2023
2 parents ff2f5b7 + 42d6abe commit 9d18da6
Show file tree
Hide file tree
Showing 7 changed files with 1,930 additions and 1,834 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/cibuild.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node: [ 14, 16, 18 ]
name: Run Tests - Node ${{ matrix.node }}
nodeVersion: [ 14, 16, 18 ]
name: Run Tests - Node ${{ matrix.nodeVersion }}
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 16
node-version: ${{ matrix.nodeVersion }}
cache: 'npm'
- name: Run tests and collect coverage
run: ./scripts/cibuild.sh
Expand Down
18 changes: 18 additions & 0 deletions .run/Run All Tests.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Run All Tests" type="mocha-javascript-test-runner">
<node-interpreter>project</node-interpreter>
<node-options>--trace-deprecation</node-options>
<mocha-package>$PROJECT_DIR$/node_modules/mocha</mocha-package>
<working-directory>$PROJECT_DIR$</working-directory>
<pass-parent-env>true</pass-parent-env>
<envs>
<env name="NODE_ENV" value="test" />
</envs>
<ui>bdd</ui>
<extra-mocha-options>--exit</extra-mocha-options>
<test-kind>DIRECTORY</test-kind>
<test-directory>$PROJECT_DIR$/test</test-directory>
<recursive>true</recursive>
<method v="2" />
</configuration>
</component>
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![NPM](https://nodei.co/npm/server-health.png?downloads=true)](https://nodei.co/npm/server-health/)

Allows to easily add a `/health` endpoint to a Restify, Express, Hapi
Allows to easily add a `/health` endpoint to a Fastify, Restify, Express, Hapi
or native node http server returning vital information about a service.

## Example output
Expand Down Expand Up @@ -70,6 +70,18 @@ server.listen(8080, function() {
});
```

For frameworks other than restify (default) specify the used framework:

```js
const fastify = require('fastify');

const server = fastify();
serverHealth.exposeHealthEndpoint(server, '/health', 'fastify');
server.listen({port: 8080}, function() {
console.log('Listening on port 8080');
});
```

### Querying from the command line

After adding the server info health endpoint to a service you can do a quick check
Expand Down
15 changes: 13 additions & 2 deletions lib/health.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ module.exports.exposeHealthEndpoint = (server, endpoint = '/health', framework =
method: 'GET',
path: endpoint,
handler: async (request, h) => {
let statusCode, status;
try {
({ statusCode, status } = await healthHandler(request.query.filter));
const { statusCode, status } = await healthHandler(request.query.filter);

return h.response(status).code(statusCode);
} catch (err) {
Expand All @@ -69,6 +68,18 @@ module.exports.exposeHealthEndpoint = (server, endpoint = '/health', framework =
});
break;

case 'fastify':
server.get(endpoint, async (req, res) => {
try {
const { statusCode, status } = await healthHandler(req.query.filter);

res.code(statusCode).send(status);
} catch (err) {
res.code(err.statusCode).send(err.toJSON());
}
});
break;

case 'restify':
// intended fallthrough
default:
Expand Down

0 comments on commit 9d18da6

Please sign in to comment.