-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add health check endpoint to Keystone's express server (#6192)
- Loading branch information
Showing
9 changed files
with
206 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
"@keystone-next/keystone": patch | ||
"@keystone-next/types": patch | ||
--- | ||
|
||
Added an optional `/_healthcheck` endpoint to Keystone's express server. | ||
|
||
You can enable it by setting `config.server.healthCheck: true` | ||
|
||
By default it will respond with `{ status: 'pass', timestamp: Date.now() }` | ||
|
||
You can also specify a custom path and JSON data: | ||
|
||
```js | ||
config({ | ||
server: { | ||
healthCheck: { | ||
path: '/my-health-check', | ||
data: { status: 'healthy' }, | ||
} | ||
} | ||
}) | ||
``` | ||
|
||
Or use a function for the `data` config to return real-time information: | ||
|
||
```js | ||
config({ | ||
server: { | ||
healthCheck: { | ||
path: '/my-health-check', | ||
data: () => ({ | ||
status: 'healthy', | ||
timestamp: Date.now(), | ||
uptime: process.uptime(), | ||
}), | ||
} | ||
} | ||
}) | ||
``` |
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,3 @@ | ||
export const defaults = { | ||
healthCheckPath: '/_healthcheck', | ||
} as const; |
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,23 @@ | ||
import type { KeystoneConfig } from '@keystone-next/types'; | ||
import { Application } from 'express'; | ||
|
||
import { defaults } from '../config/defaults'; | ||
|
||
type AddHealthCheckArgs = { config: KeystoneConfig; server: Application }; | ||
|
||
export const addHealthCheck = async ({ config, server }: AddHealthCheckArgs) => { | ||
if (!config.server?.healthCheck) return; | ||
const healthCheck = config.server.healthCheck === true ? {} : config.server.healthCheck; | ||
|
||
const path = healthCheck.path || defaults.healthCheckPath; | ||
|
||
server.use(path, (req, res) => { | ||
const data = (typeof healthCheck.data === 'function' | ||
? healthCheck.data() | ||
: healthCheck.data) || { | ||
status: 'pass', | ||
timestamp: Date.now(), | ||
}; | ||
res.json(data); | ||
}); | ||
}; |
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,74 @@ | ||
import { createSchema, list } from '@keystone-next/keystone/schema'; | ||
import { text } from '@keystone-next/fields'; | ||
import { setupTestRunner } from '@keystone-next/testing'; | ||
import supertest from 'supertest'; | ||
import { apiTestConfig } from './utils'; | ||
|
||
const makeRunner = (healthCheck: any) => | ||
setupTestRunner({ | ||
config: apiTestConfig({ | ||
lists: createSchema({ User: list({ fields: { name: text() } }) }), | ||
server: { healthCheck }, | ||
}), | ||
}); | ||
|
||
test( | ||
'No health check', | ||
makeRunner(undefined)(async ({ app }) => { | ||
await supertest(app).get('/_healthcheck').set('Accept', 'application/json').expect(404); | ||
}) | ||
); | ||
|
||
test( | ||
'Default health check', | ||
makeRunner(true)(async ({ app }) => { | ||
const { text } = await supertest(app) | ||
.get('/_healthcheck') | ||
.set('Accept', 'application/json') | ||
.expect('Content-Type', /json/) | ||
.expect(200); | ||
expect(JSON.parse(text)).toEqual({ | ||
status: 'pass', | ||
timestamp: expect.any(Number), | ||
}); | ||
}) | ||
); | ||
|
||
test( | ||
'Custom path', | ||
makeRunner({ path: '/custom' })(async ({ app }) => { | ||
const { text } = await supertest(app) | ||
.get('/custom') | ||
.set('Accept', 'application/json') | ||
.expect('Content-Type', /json/) | ||
.expect(200); | ||
expect(JSON.parse(text)).toEqual({ | ||
status: 'pass', | ||
timestamp: expect.any(Number), | ||
}); | ||
}) | ||
); | ||
|
||
test( | ||
'Custom data: object', | ||
makeRunner({ data: { foo: 'bar' } })(async ({ app }) => { | ||
const { text } = await supertest(app) | ||
.get('/_healthcheck') | ||
.set('Accept', 'application/json') | ||
.expect('Content-Type', /json/) | ||
.expect(200); | ||
expect(JSON.parse(text)).toEqual({ foo: 'bar' }); | ||
}) | ||
); | ||
|
||
test( | ||
'Custom data: function', | ||
makeRunner({ data: () => ({ foo: 'bar' }) })(async ({ app }) => { | ||
const { text } = await supertest(app) | ||
.get('/_healthcheck') | ||
.set('Accept', 'application/json') | ||
.expect('Content-Type', /json/) | ||
.expect(200); | ||
expect(JSON.parse(text)).toEqual({ foo: 'bar' }); | ||
}) | ||
); |
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
93f1e5d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: