Skip to content

Commit

Permalink
Added health controller
Browse files Browse the repository at this point in the history
  • Loading branch information
BradyMitch committed Oct 28, 2024
1 parent cd62c2e commit fbbb46b
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 17 deletions.
1 change: 1 addition & 0 deletions .env-template
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ S3_BUCKET=dats-bucket-dev
CHES_CLIENT_ID=
CHES_CLIENT_SECRET=
CHES_ENVIRONMENT=dev
CHES_URL=https://ches-dev.api.gov.bc.ca/api/v1

ENVIRONMENT=dev

Expand Down
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"dependencies": {
"@aws-sdk/client-s3": "3.677.0",
"@bcgov/citz-imb-express-utilities": "1.0.0",
"@bcgov/citz-imb-express-utilities": "1.0.1-beta",
"@bcgov/citz-imb-sso-js-core": "1.0.0",
"amqplib": "0.10.4",
"cookie-parser": "1.4.7",
Expand Down
2 changes: 2 additions & 0 deletions backend/src/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const {
CHES_CLIENT_SECRET,
CHES_REALM = "comsvcauth",
CHES_ENVIRONMENT = "dev",
CHES_URL = "https://ches-dev.api.gov.bc.ca/api/v1",
} = process.env;

const AUTH_URL = {
Expand Down Expand Up @@ -56,4 +57,5 @@ export default {
CHES_CLIENT_ID,
CHES_CLIENT_SECRET,
CHES_TOKEN_ENDPOINT,
CHES_URL,
};
44 changes: 44 additions & 0 deletions backend/src/modules/ches/controllers/health.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { Request, Response } from "express";
import { errorWrapper, safePromise, standardResponse } from "@bcgov/citz-imb-express-utilities";
import { fetchToken } from "../utils";
import { ENV } from "src/config";

const { CHES_URL } = ENV;

export const health = errorWrapper(async (req: Request, res: Response) => {
// Fetch token for CHES auth.
const [fetchTokenError, tokenResponse] = await fetchToken();
if (fetchTokenError) throw fetchTokenError;

const { access_token } = await tokenResponse.json();

// Get access token.
if (!access_token) throw new Error("Missing access_token in CHES token endpoint response.");

// Call CHES /health endpoint using safePromise
const [healthError, healthResponse] = await safePromise(
fetch(`${CHES_URL}/health`, {
method: "GET",
headers: {
Authorization: `Bearer ${access_token}`,
},
}),
);
if (healthError) throw healthError;

// Check if the response is OK
if (!healthResponse?.ok) {
const errorMessage = `CHES health endpoint returned an error: ${healthResponse?.statusText}`;
throw new Error(errorMessage);
}

const data = await healthResponse.json();

const result = standardResponse(
{ data, message: "CHES connection successful", success: true },
req,
);

// Return health check result
return res.status(200).json(result);
});
1 change: 1 addition & 0 deletions backend/src/modules/ches/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./health";
21 changes: 5 additions & 16 deletions backend/src/modules/ches/utils/fetchToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,7 @@ import { ENV } from "src/config";

const { CHES_CLIENT_ID, CHES_CLIENT_SECRET, CHES_TOKEN_ENDPOINT } = ENV;

type TokenResponse = {
access_token: string;
expires_in: number;
refresh_expires_in: number;
token_type: string;
"not-before-policy": number;
scope: string;
};

export const fetchToken = async (): Promise<[Error | null, TokenResponse | null]> => {
export const fetchToken = async (): Promise<[Error, null] | [null, Response]> => {
if (!CHES_CLIENT_ID || !CHES_CLIENT_SECRET || !CHES_TOKEN_ENDPOINT) {
return [new Error("Missing CHES configuration variables"), null];
}
Expand All @@ -21,9 +12,9 @@ export const fetchToken = async (): Promise<[Error | null, TokenResponse | null]
params.append("grant_type", "client_credentials");
params.append("client_id", CHES_CLIENT_ID);
params.append("client_secret", CHES_CLIENT_SECRET);
params.append("scope", "openid");

// Use safePromise with fetch
const [err, response] = await safePromise<TokenResponse>(
const [err, response] = await safePromise(
fetch(CHES_TOKEN_ENDPOINT, {
method: "POST",
headers: {
Expand All @@ -34,8 +25,6 @@ export const fetchToken = async (): Promise<[Error | null, TokenResponse | null]
);

if (err) return [err, null];

// Check if the response contains an access token
if (response?.access_token) return [null, response];
return [new Error("Failed to retrieve CHES token"), null];
if (!response) return [new Error("fetchToken response returned null"), null];
return [null, response];
};

0 comments on commit fbbb46b

Please sign in to comment.