From adce4057dc14f800da7b0923dce0b09eaf1d5355 Mon Sep 17 00:00:00 2001 From: Tony Date: Fri, 16 Aug 2024 00:50:31 +0800 Subject: [PATCH] fix(core): healthz bypass cache (#16455) --- lib/registry.test.ts | 8 ++++++++ lib/registry.ts | 3 ++- lib/routes/healthz.ts | 8 ++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 lib/routes/healthz.ts diff --git a/lib/registry.test.ts b/lib/registry.test.ts index d168eb599..a2a2eb8fc 100644 --- a/lib/registry.test.ts +++ b/lib/registry.test.ts @@ -29,4 +29,12 @@ describe('registry', () => { const response = await app.request('/favicon.ico'); expect(response.status).toBe(200); }); + + // healthz + it('/healthz', async () => { + const response = await app.request('/healthz'); + expect(response.status).toBe(200); + expect(response.headers.get('cache-control')).toBe('no-cache'); + expect(await response.text()).toBe('ok'); + }); }); diff --git a/lib/registry.ts b/lib/registry.ts index 9711fb8b3..0ac903323 100644 --- a/lib/registry.ts +++ b/lib/registry.ts @@ -7,6 +7,7 @@ import { serveStatic } from '@hono/node-server/serve-static'; import { config } from '@/config'; import index from '@/routes/index'; +import healthz from '@/routes/healthz'; import robotstxt from '@/routes/robots.txt'; import metrics from '@/routes/metrics'; @@ -100,7 +101,7 @@ for (const namespace in namespaces) { } app.get('/', index); -app.get('/healthz', (ctx) => ctx.text('ok')); +app.get('/healthz', healthz); app.get('/robots.txt', robotstxt); if (config.debugInfo) { // Only enable tracing in debug mode diff --git a/lib/routes/healthz.ts b/lib/routes/healthz.ts new file mode 100644 index 000000000..96f15744f --- /dev/null +++ b/lib/routes/healthz.ts @@ -0,0 +1,8 @@ +import type { Handler } from 'hono'; + +const handler: Handler = (ctx) => { + ctx.header('Cache-Control', 'no-cache'); + return ctx.text('ok'); +}; + +export default handler;