Skip to content

Commit fccd1cf

Browse files
committed
Add health check endpoints
1 parent eff418f commit fccd1cf

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import express from 'express';
22
import dotenv from 'dotenv';
33
import authRouter from './routes/auth';
44
import fileRouter from './routes/file';
5+
import healthRouter from './routes/health';
56
import cors from 'cors';
67

78
dotenv.config();
@@ -14,6 +15,8 @@ app.use("/api/auth", authRouter);
1415

1516
app.use("/api/files", fileRouter);
1617

18+
app.use("/health", healthRouter);
19+
1720
// Serve static files
1821
app.use('/pdfjs', express.static("public/pdfjs"));
1922
app.use('/build', express.static("public/build"));
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Request, Response } from "express";
2+
import prisma from "../../prismaClient/client";
3+
4+
export const detailedSeverHealthInfo = async (req: Request, res: Response) => {
5+
6+
const healthcheck = {
7+
message: "OK",
8+
uptime: process.uptime(),
9+
timeStamp: new Date().toISOString(),
10+
services: {
11+
database: "unknown"
12+
}
13+
}
14+
15+
try {
16+
// Prisma DB check
17+
await prisma.$queryRaw`SELECT 1`;
18+
healthcheck.services.database = "connected";
19+
20+
res.status(200).json(healthcheck);
21+
} catch (error: any) {
22+
healthcheck.services.database = "disconnected";
23+
healthcheck.message = error.message;
24+
res.status(503).json(healthcheck);
25+
}
26+
}

src/modules/health/simpleHealth.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Request, Response } from "express";
2+
3+
export const simpleSeverHealthInfo = (req: Request, res: Response) => {
4+
res.status(200).json({
5+
status: "OK",
6+
message: "👍 Sever is healthy"
7+
})
8+
}

src/routes/health.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Express from "express"
2+
import { simpleSeverHealthInfo } from "../modules/health/simpleHealth";
3+
import { detailedSeverHealthInfo } from "../modules/health/detailedHealth";
4+
5+
const healthRouter = Express.Router();
6+
7+
healthRouter.get("/", simpleSeverHealthInfo);
8+
healthRouter.get('/detailed', detailedSeverHealthInfo);
9+
10+
export default healthRouter;

0 commit comments

Comments
 (0)