Skip to content

Commit 6c04363

Browse files
committed
feat: add internal healthcheck api route for directus
1 parent 129a593 commit 6c04363

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/env/schema.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const serverSchema = z.object({
1010
GITHUB_API_TOKEN: z.string(),
1111
DIRECTUS_API_TOKEN: z.string(),
1212
DIRECTUS_REVALIDATE_KEY: z.string(),
13+
HEALTHCHECK_SECRET: z.string(), // Added for healthcheck route
1314
NODE_ENV: z.enum(["development", "test", "production"]),
1415
TITLE: z.preprocess((value) => {
1516
if (value === undefined || value === "") return null;

src/pages/api/healthcheck.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { NextApiRequest, NextApiResponse } from "next";
2+
import directus from "@/utils/directus";
3+
import { env } from "@/env/server.mjs";
4+
import { readItems } from "@directus/sdk";
5+
6+
export default async function handler(
7+
req: NextApiRequest,
8+
res: NextApiResponse,
9+
) {
10+
const secret = req.headers["authorization"];
11+
if (typeof secret !== "string" || secret !== env.HEALTHCHECK_SECRET) {
12+
return res.status(401).json({ error: "Unauthorized" });
13+
}
14+
15+
try {
16+
// Try a simple Directus API call (fetch one project)
17+
await directus.request(readItems("project", { limit: 1 }));
18+
return res.status(200).json({ status: "ok" });
19+
} catch (error) {
20+
return res
21+
.status(500)
22+
.json({ error: "Directus unhealthy", details: String(error) });
23+
}
24+
}

0 commit comments

Comments
 (0)