-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd62c2e
commit fbbb46b
Showing
6 changed files
with
54 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./health"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters