Skip to content

Commit 6591a7b

Browse files
committed
feat: add health and readiness endpoints
1 parent 0b88f3a commit 6591a7b

3 files changed

Lines changed: 17 additions & 4 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "etherproxy",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"description": "",
55
"main": "dist/index.js",
66
"bin": {

src/index.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { Arrays, Objects } from 'cafe-utility'
44
import { IncomingMessage, ServerResponse, createServer } from 'http'
5+
import fetch from 'node-fetch'
56
import { RequestContext, ResponseContext } from './types'
67
import { fetchWithTimeout, respondWithFetchPromise } from './utility'
78

@@ -13,7 +14,19 @@ function main() {
1314
const expiry = Arrays.requireNumberArgument(process.argv, 'expiry')
1415

1516
const fastIndex = Objects.createFastIndex()
16-
const server = createServer((request: IncomingMessage, response: ServerResponse) => {
17+
const server = createServer(async (request: IncomingMessage, response: ServerResponse) => {
18+
if (request.url === '/health' || request.url === '/readiness') {
19+
try {
20+
await fetch(target, { timeout: 10_000 })
21+
response.statusCode = 200
22+
response.end('200 OK')
23+
} catch (error) {
24+
console.error(error)
25+
response.statusCode = 503
26+
response.end('503 Service Unavailable')
27+
}
28+
return
29+
}
1730
const chunks: Buffer[] = []
1831
request.on('data', (chunk: Buffer) => {
1932
chunks.push(chunk)

0 commit comments

Comments
 (0)