Skip to content

Commit

Permalink
feat(Docker): Add readiness check /ready
Browse files Browse the repository at this point in the history
  • Loading branch information
cblanc committed Dec 9, 2020
1 parent e820b73 commit 890e33b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/app/controllers/utils_controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
import { Handler } from "../types/express";
import { query } from "../models/base";
import { NotReadyError } from "../lib/errors";

export const ping: Handler = (request, response, next) => {
export const ping: Handler = (_, response, next) => {
response.jsonApiResponse = {
status: 200,
result: "pong",
};
next();
};

export const ready: Handler = async (_, response, next) => {
try {
await query("SELECT 1");
} catch (error) {
return next(new NotReadyError());
}
response.jsonApiResponse = {
status: 200,
result: "Ready",
};
next();
};
6 changes: 6 additions & 0 deletions src/app/lib/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,9 @@ export class OutcodeNotFoundError extends PostcodesioHttpError {
super(404, "Outcode not found");
}
}

export class NotReadyError extends PostcodesioHttpError {
constructor() {
super(500, "Service not ready. Database is not available");
}
}
1 change: 1 addition & 0 deletions src/config/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as terminatedPostcodes from "../app/controllers/terminated_postcodes_co
export const routes = (app: Express): void => {
app.get("/", pages.home);
app.get("/ping", utils.ping);
app.get("/ready", utils.ready);
app.get("/about", pages.about);
app.get("/docs", pages.documentation);
app.get("/explore", pages.explore);
Expand Down
9 changes: 9 additions & 0 deletions test/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ describe("Misc", () => {
});

describe("Utils", () => {
describe("Health", () => {
it("should return 200 if DB available", async () => {
const { body } = await request(app)
.get("/ready")
.expect(200)
.expect("Content-Type", /json/);
assert.equal(body.result, "Ready");
});
});
describe("Ping", () => {
it("should pong", async () => {
const { body } = await request(app)
Expand Down

0 comments on commit 890e33b

Please sign in to comment.