Skip to content

Commit

Permalink
feat: unprotected unversioned health route
Browse files Browse the repository at this point in the history
  • Loading branch information
adriantr committed Mar 21, 2023
1 parent 5505158 commit 3fe7e2c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/plugins/remote-cache/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { FastifyInstance } from 'fastify'
import { badRequest, unauthorized } from '@hapi/boom'
import { getArtifact, putArtifact, artifactsEvents, headArtifact, getStatus } from './routes'
import {
getArtifact,
putArtifact,
artifactsEvents,
headArtifact,
getStatus,
getHealth,
} from './routes'
import { createLocation } from './storage'
import { STORAGE_PROVIDERS } from '../../env'

Expand Down Expand Up @@ -33,6 +40,10 @@ async function turboRemoteCache(
let authHeader = request.headers['authorization']
authHeader = Array.isArray(authHeader) ? authHeader.join() : authHeader

if (request.url.includes('health')) {
return
}

if (!authHeader) {
throw badRequest(`Missing Authorization header`)
}
Expand All @@ -58,6 +69,10 @@ async function turboRemoteCache(
}),
)

await instance.register(async i => {
i.route(getHealth)
})

await instance.register(
async function (i) {
i.route(getArtifact)
Expand Down
14 changes: 14 additions & 0 deletions src/plugins/remote-cache/routes/get-health.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { Server } from 'http'
import type { RouteOptions, RawReplyDefaultExpression, RawRequestDefaultExpression } from 'fastify'

export const getHealth: RouteOptions<
Server,
RawRequestDefaultExpression,
RawReplyDefaultExpression
> = {
method: 'GET',
url: '/health',
async handler(req, reply) {
reply.send('Server is running 🙌')
},
}
1 change: 1 addition & 0 deletions src/plugins/remote-cache/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { headArtifact } from './head-artifact'
export { putArtifact } from './put-artifact'
export { artifactsEvents } from './artifacts-events'
export { getStatus } from './get-status'
export { getHealth } from './get-health'
8 changes: 8 additions & 0 deletions test/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,12 @@ test(`local'`, async t => {
t2.equal(response.statusCode, 200)
t2.same(response.json(), { status: 'enabled' })
})
t.test('should return 200 when GET health is called', async t2 => {
t2.plan(1)
const response = await app.inject({
method: 'GET',
url: `/health`,
})
t2.equal(response.statusCode, 200)
})
})

0 comments on commit 3fe7e2c

Please # to comment.