Skip to content

Your favorite shift management application!

chore: implement health check endpoint and update Dockerfile health check command

panteLx710b98dfc151
diff --git a/Dockerfile b/Dockerfile
index e4b9ee0..7493a4e 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -77,9 +77,9 @@ RUN echo "$VERSION" > /app/.version
# Expose port
EXPOSE 3000
-# Health check
+# Health check using dedicated health endpoint
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
- CMD node -e "require('http').get('http://localhost:3000', (r) => {r.resume(); r.on('end', () => process.exit(r.statusCode === 200 ? 0 : 1))}).on('error', () => process.exit(1))"
+ CMD node -e "require('http').get('http://localhost:3000/api/health', (r) => {let d='';r.on('data',c=>d+=c);r.on('end',()=>process.exit(r.statusCode===200?0:1))}).on('error',()=>process.exit(1))"
# Use dumb-init to handle signals properly
ENTRYPOINT ["/usr/bin/dumb-init", "--"]
diff --git a/app/api/health/route.ts b/app/api/health/route.ts
new file mode 100644
index 0000000..9513b2a
--- /dev/null
+++ b/app/api/health/route.ts
@@ -0,0 +1,78 @@
+import { NextRequest, NextResponse } from "next/server";
+import { db } from "@/lib/db";
+import { sql } from "drizzle-orm";
+import { readFile } from "fs/promises";
+import { join } from "path";
+
+export const dynamic = "force-dynamic";
+
+interface HealthStatus {
+ status: "healthy" | "unhealthy";
+ timestamp: string;
+ version: string;
+ checks: {
+ database: {
+ status: "ok" | "error";
+ message?: string;
+ };
+ server: {
+ status: "ok";
+ uptime: number;
+ };
+ };
+}
+
+export async function GET(request: NextRequest) {
+ const startTime = Date.now();
+ const health: HealthStatus = {
+ status: "healthy",
+ timestamp: new Date().toISOString(),
+ version: "unknown",
+ checks: {
+ database: {
+ status: "ok",
+ },
+ server: {
+ status: "ok",
+ uptime: process.uptime(),
+ },
+ },
+ };
+
+ // Check version
+ try {
+ const versionPath = join(process.cwd(), ".version");
+ const version = await readFile(versionPath, "utf-8");
+ health.version = version.trim();
+ } catch (error) {
+ // Version file might not exist in dev mode
+ health.version = process.env.NODE_ENV === "production" ? "unknown" : "dev";
+ }
+
+ // Check database connection
+ try {
+ db.run(sql`SELECT 1`);
+ health.checks.database.status = "ok";
+ } catch (error) {
+ health.status = "unhealthy";
+ health.checks.database.status = "error";
+ health.checks.database.message =
+ error instanceof Error ? error.message : "Database connection failed";
+ }
+
+ const responseTime = Date.now() - startTime;
+ const status = health.status === "healthy" ? 200 : 503;
+
+ return NextResponse.json(
+ {
+ ...health,
+ responseTime: `${responseTime}ms`,
+ },
+ {
+ status,
+ headers: {
+ "Cache-Control": "no-cache, no-store, must-revalidate",
+ },
+ }
+ );
+}
diff --git a/proxy.ts b/proxy.ts
index 7cae7f2..d3754f6 100644
--- a/proxy.ts
+++ b/proxy.ts
@@ -107,6 +107,7 @@ export async function proxy(request: NextRequest) {
"/api/auth", // Better Auth API routes
"/api/version", // Version info (always public)
"/api/releases", // Changelog/releases (always public)
+ "/api/health", // Health check endpoint
];
// Check if the current route is public