diff --git a/.env.example b/.env.example index 1c63511..e143a54 100644 --- a/.env.example +++ b/.env.example @@ -19,4 +19,7 @@ NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY= NEXT_PUBLIC_CLERK_SIGN_IN_URL=/sign-in NEXT_PUBLIC_CLERK_SIGN_UP_URL=/sign-up NEXT_PUBLIC_CLERK_AFTER_SIGN_IN_URL=/ -NEXT_PUBLIC_CLERK_AFTER_SIGN_UP_URL=/ \ No newline at end of file +NEXT_PUBLIC_CLERK_AFTER_SIGN_UP_URL=/ + +# Misc +HEALTHCHECK_TOKEN= \ No newline at end of file diff --git a/src/env.mjs b/src/env.mjs index a9a817a..162a8df 100644 --- a/src/env.mjs +++ b/src/env.mjs @@ -28,6 +28,9 @@ export const env = createEnv({ // Clerk CLERK_SECRET_KEY: z.string(), NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY: z.string(), + + // Misc + HEALTHCHECK_TOKEN: z.string() }, /** @@ -62,6 +65,7 @@ export const env = createEnv({ NEXT_PUBLIC_CLERK_SIGN_UP_URL: process.env.NEXT_PUBLIC_CLERK_SIGN_UP_URL, NEXT_PUBLIC_CLERK_AFTER_SIGN_IN_URL: process.env.NEXT_PUBLIC_CLERK_AFTER_SIGN_IN_URL, NEXT_PUBLIC_CLERK_AFTER_SIGN_UP_URL: process.env.NEXT_PUBLIC_CLERK_AFTER_SIGN_UP_URL, + HEALTHCHECK_TOKEN: process.env.HEALTHCHECK_TOKEN }, /** * Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. diff --git a/src/middleware.ts b/src/middleware.ts index 05f75ab..77ed62a 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -1,7 +1,7 @@ import { authMiddleware } from "@clerk/nextjs"; export default authMiddleware({ - publicRoutes: ["/", "/sign-up"] + publicRoutes: ["/", "/sign-up", "/api/healthcheck"] }); export const config = { diff --git a/src/pages/api/healthcheck/index.ts b/src/pages/api/healthcheck/index.ts index 8293e79..fdaf8c1 100644 --- a/src/pages/api/healthcheck/index.ts +++ b/src/pages/api/healthcheck/index.ts @@ -1,5 +1,27 @@ +import type { NextApiRequest, NextApiResponse } from "next"; +import { env } from "../../../env.mjs"; +import { db } from "../../../server/db/drizzle"; +import { sql } from "drizzle-orm" +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + const accessToken = req.headers.authorization; -export default function handler(req: NextApiRequest, res: NextApiResponse) { - -} \ No newline at end of file + if (accessToken !== env.HEALTHCHECK_TOKEN) { + res.status(403).json({ message: "Forbidden" }); + return; + } + + let isDatabaseHealthy = false; + + try { + await db.execute(sql`SELECT 1;`) + isDatabaseHealthy = true; + } + catch (err) { + console.error(err); + } + + res.json({ + isDatabaseHealthy, + }); +}