Skip to content

Commit

Permalink
Added healthcheck api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Neo-Ciber94 committed Nov 23, 2023
1 parent f25fe0a commit b620b01
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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=/
NEXT_PUBLIC_CLERK_AFTER_SIGN_UP_URL=/

# Misc
HEALTHCHECK_TOKEN=
4 changes: 4 additions & 0 deletions src/env.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
},

/**
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { authMiddleware } from "@clerk/nextjs";

export default authMiddleware({
publicRoutes: ["/", "/sign-up"]
publicRoutes: ["/", "/sign-up", "/api/healthcheck"]
});

export const config = {
Expand Down
28 changes: 25 additions & 3 deletions src/pages/api/healthcheck/index.ts
Original file line number Diff line number Diff line change
@@ -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) {

}
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,
});
}

0 comments on commit b620b01

Please sign in to comment.