From fe9a14fede8ca98fbfd59f76ad87e8fd25a6c478 Mon Sep 17 00:00:00 2001 From: rajmayank Date: Wed, 9 Oct 2019 04:56:23 +0530 Subject: [PATCH] issue-31: Added health check endpoint --- src/app.ts | 5 ++- src/controllers/healthCheckController.ts | 46 ++++++++++++++++++++++++ src/routes/healthCheckRoutes.ts | 17 +++++++++ swagger.json | 19 ++++++++++ 4 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 src/controllers/healthCheckController.ts create mode 100644 src/routes/healthCheckRoutes.ts diff --git a/src/app.ts b/src/app.ts index 004b532..ffc2a34 100644 --- a/src/app.ts +++ b/src/app.ts @@ -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; @@ -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)); } } diff --git a/src/controllers/healthCheckController.ts b/src/controllers/healthCheckController.ts new file mode 100644 index 0000000..7fc2af8 --- /dev/null +++ b/src/controllers/healthCheckController.ts @@ -0,0 +1,46 @@ +import { Request, Response } from "express"; +import * as mongoose from "mongoose"; + + +const mongooseConnectionStates = { + 0: "disconnected", + 1: "connected", + 2: "connecting", + 3: "disconnecting" +} + +export class HealthCheckController { + + public check = (req: Request, res: Response) => { + + let isServerHealthy = true + + let mongooseState = mongoose.connection.readyState; + if (mongooseState != 1) + isServerHealthy = false + + // Add additional checks here + + let healthStatus = { + 'status': isServerHealthy ? 'healthy' : 'unhealthy', + 'services': { + 'db_connection': mongooseConnectionStates[mongooseState] + } + } + + if (isServerHealthy) { + return res + .status(200) + .type('json') + .send(healthStatus) + } else { + return res + .status(500) + .type('json') + .send(healthStatus) + } + }; + +} + +export const healthCheckController = new HealthCheckController(); \ No newline at end of file diff --git a/src/routes/healthCheckRoutes.ts b/src/routes/healthCheckRoutes.ts new file mode 100644 index 0000000..3c629a1 --- /dev/null +++ b/src/routes/healthCheckRoutes.ts @@ -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; diff --git a/swagger.json b/swagger.json index f739868..1e1e745 100644 --- a/swagger.json +++ b/swagger.json @@ -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": [