Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue-31: Added health check endpoint #120

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 = {
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();
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