Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import * as bodyParser from "body-parser";
import * as express from "express";
import * as helmet from "helmet";
import * as swaggerUi from "swagger-ui-express";
import * as swaggerDocument from "./../swagger.json";

import { fiddleRoutes } from "./routes/fiddleRoutes";
import { healthCheckRoutes } from "./routes/healthCheckRoutes";

class App {
public app: express.Application;
Expand All @@ -16,8 +18,9 @@ class App {
private config(): void {
this.app.use(helmet());
this.app.use(bodyParser.json()); // parse json from request body
this.app.use("/health", healthCheckRoutes);
this.app.use("/fiddles", fiddleRoutes);
// this.app.use("/docs", swaggerUi.serve, swaggerUi.setup(require("../swagger.json")));
this.app.use("/docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
}
}

Expand Down
46 changes: 46 additions & 0 deletions src/controllers/healthCheckController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Request, Response } from "express";
import * as mongoose from "mongoose";

const mongooseConnectionStates: { [key: string]: any } = {
0: "disconnected",
1: "connected",
2: "connecting",
3: "disconnecting",
};

export class HealthCheckController {

public check = (req: Request, res: Response) => {

let isServerHealthy = true;

const mongooseState = mongoose.connection.readyState;
if (mongooseState !== 1) {
isServerHealthy = false;
}

// Add additional checks here

const healthStatus = {
services: {
db_connection: mongooseConnectionStates[mongooseState],
},
status: isServerHealthy ? "healthy" : "unhealthy",
};

if (isServerHealthy) {
return res
.status(200)
.type("json")
.send(healthStatus);
} else {
return res
.status(500)
.type("json")
.send(healthStatus);
}
}

}

export const healthCheckController = new HealthCheckController();
17 changes: 17 additions & 0 deletions src/routes/healthCheckRoutes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as express from "express";
import { healthCheckController } from "../controllers/healthCheckController";

class HealthCheckRoutes {
public router: express.Router = express.Router();

constructor() {
this.config();
}

private config(): void {

this.router.get("/", healthCheckController.check);
}
}

export const healthCheckRoutes = new HealthCheckRoutes().router;
19 changes: 19 additions & 0 deletions swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@
}
},
"paths": {
"/health": {
"get": {
"tags": [
"health"
],
"summary": "Get the status of the service",
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "Service is operating normally"
},
"500": {
"description": "Service is not operating normally"
}
}
}
},
"/users": {
"post": {
"tags": [
Expand Down