Skip to content

Commit ae5d2f9

Browse files
committed
chore: added health-checks
1 parent 7e1af13 commit ae5d2f9

File tree

9 files changed

+124
-17
lines changed

9 files changed

+124
-17
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// SPDX-FileCopyrightText: 2025 2025 INDUSTRIA DE DISEÑO TEXTIL S.A. (INDITEX S.A.)
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
import { Request, Response } from "express";
6+
import { getAzureWebPubsubServer, isStorageConnected } from "../../../store.js";
7+
8+
export const getLivenessHealthCheckController =
9+
() =>
10+
async (req: Request, res: Response): Promise<void> => {
11+
const testRoomId = "test-readiness-room";
12+
13+
if (!getAzureWebPubsubServer) {
14+
res
15+
.status(500)
16+
.json({ error: "Azure Web PubSub store instance not found" });
17+
return;
18+
}
19+
20+
const url = await getAzureWebPubsubServer().clientConnect(testRoomId);
21+
22+
if (!url) {
23+
res
24+
.status(500)
25+
.json({ status: "Azure Web Pubsub store failed to connect." });
26+
return;
27+
}
28+
29+
if (!isStorageConnected()) {
30+
res.status(500).json({ status: "Azure Blob Storage failed to connect." });
31+
return;
32+
}
33+
34+
res.status(200).json({ status: "OK" });
35+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// SPDX-FileCopyrightText: 2025 2025 INDUSTRIA DE DISEÑO TEXTIL S.A. (INDITEX S.A.)
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
import { Request, Response } from "express";
6+
import {
7+
getAzureWebPubsubServer,
8+
isStorageInitialized,
9+
} from "../../../store.js";
10+
11+
export const getReadinessHealthCheckController =
12+
() =>
13+
async (req: Request, res: Response): Promise<void> => {
14+
if (!getAzureWebPubsubServer || !isStorageInitialized()) {
15+
res.status(500).json({ status: "Not initialized" });
16+
return;
17+
}
18+
19+
res.status(200).json({ status: "OK" });
20+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// SPDX-FileCopyrightText: 2025 2025 INDUSTRIA DE DISEÑO TEXTIL S.A. (INDITEX S.A.)
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
import { Request, Response } from "express";
6+
import {
7+
getAzureWebPubsubServer,
8+
isStorageInitialized,
9+
} from "../../../store.js";
10+
11+
export const getStartUpHealthCheckController =
12+
() =>
13+
(req: Request, res: Response): void => {
14+
if (!getAzureWebPubsubServer || !isStorageInitialized()) {
15+
res.status(500).json({ status: "Not initialized" });
16+
return;
17+
}
18+
19+
res.status(200).json({ status: "OK" });
20+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-FileCopyrightText: 2025 2025 INDUSTRIA DE DISEÑO TEXTIL S.A. (INDITEX S.A.)
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
5+
import { Express, Router } from "express";
6+
import { getLivenessHealthCheckController } from "./controllers/getLivenessHealthCheck.js";
7+
import { getReadinessHealthCheckController } from "./controllers/getReadinessHealthCheck.js";
8+
import { getStartUpHealthCheckController } from "./controllers/getStartUpHealthCheck.js";
9+
10+
const router: Router = Router();
11+
12+
export function getHealthChecksRouter() {
13+
return router;
14+
}
15+
16+
export function setupHealthChecksRouter(app: Express) {
17+
const router: Router = Router();
18+
19+
// Setup router routes
20+
router.get(`/liveness`, getLivenessHealthCheckController());
21+
router.get(`/readiness`, getReadinessHealthCheckController());
22+
router.get(`/startup`, getStartUpHealthCheckController());
23+
24+
app.use("/v1", router);
25+
}

code/src/api/v1/controllers/getHealth.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

code/src/api/v1/router.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import { Express, Router } from "express";
66
import multer from "multer";
77
import { getServiceConfig } from "../../config/config.js";
8-
import { getHealthController } from "./controllers/getHealth.js";
98
import { getRoomConnectController } from "./controllers/getRoomConnect.js";
109
import { getImageController } from "./controllers/getImage.js";
1110
import { postUploadImageController } from "./controllers/postUploadImage.js";
@@ -37,9 +36,6 @@ export function setupApiV1Router(app: Express) {
3736
// Setup cors
3837
const cors = getCorsMiddleware();
3938

40-
// Setup router routes
41-
router.get(`/health`, cors, getHealthController());
42-
4339
// Room handling API
4440
router.use(getAzureWebPubsubServer().getExpressJsMiddleware());
4541
router.get(

code/src/app.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { setupHttpResponseHeadersMiddleware } from "./middlewares/http-response-
1010
import { setupBodyParserMiddleware } from "./middlewares/body-parser.js";
1111
import { setupApiV1Router } from "./api/v1/router.js";
1212
import { setupApiV2Router } from "./api/v2/router.js";
13+
import { setupHealthChecksRouter } from "./api/health-checks/router.js";
1314
import { setLogLevel } from "@azure/logger";
1415
import { getLogger } from "./logger/logger.js";
1516

@@ -37,6 +38,9 @@ export function setupApp() {
3738
setupHttpResponseHeadersMiddleware(app);
3839
setupBodyParserMiddleware(app);
3940

41+
// Setup Health Checks Router
42+
setupHealthChecksRouter(app);
43+
4044
// Setup Routers
4145
setupApiV1Router(app);
4246
setupApiV2Router(app);

code/src/server.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import path from "node:path";
99
import { fileURLToPath } from "node:url";
1010
import { getLogger, setupLogger } from "./logger/logger.js";
1111
import { setupApp } from "./app.js";
12-
import { setupStore } from "./store.js";
12+
import { setupStorage, setupStore } from "./store.js";
1313
import { validateServiceConfig } from "./validate.js";
1414

1515
// __dirname equivalent in ESM
@@ -27,6 +27,8 @@ if (!config) {
2727
process.exit(1);
2828
}
2929

30+
// Setup the Azure Web Pubsub store
31+
await setupStorage();
3032
// Setup the Azure Web Pubsub store
3133
setupStore();
3234

code/src/store.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,23 @@ function extractImageIdFromNode(images: string[], node: any) {
4141
}
4242
}
4343

44-
async function setupStorage() {
44+
export const isStorageInitialized = () => storageInitialized;
45+
46+
export const isStorageConnected = async () => {
47+
if (!storageInitialized) {
48+
await setupStorage();
49+
}
50+
51+
if (!containerClient || !blobServiceClient) {
52+
return null;
53+
}
54+
55+
const exists = await containerClient.exists();
56+
57+
return exists;
58+
};
59+
60+
export async function setupStorage() {
4561
const config = getServiceConfig();
4662

4763
const {

0 commit comments

Comments
 (0)