Skip to content

Commit

Permalink
Implemented logic to load OpenAPI validator spec at app load
Browse files Browse the repository at this point in the history
Implemented logic to load OpenAPI validator spec at app load to improve latency issue
Removed unused import from some files
Added /status routs to debug version of app deployed
Added log for OpenAPI validator failure
Added log for layer2 config message
Added action in latency logs
  • Loading branch information
Abhishek Y authored and Abhishek Y committed Jun 29, 2024
1 parent 223afb6 commit 62fa683
Show file tree
Hide file tree
Showing 21 changed files with 334 additions and 131 deletions.
7 changes: 7 additions & 0 deletions config/config-sample-client-localhost.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,10 @@ app:

useHMACForWebhook: false
sharedKeyForWebhookHMAC: ""

useLayer2Config: false
mandateLayer2Config: false

openAPIValidator:
cachedFileLimit: 5
initialFilesToCache: ""
7 changes: 7 additions & 0 deletions config/config-sample-network-localhost.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,10 @@ app:

useHMACForWebhook: false
sharedKeyForWebhookHMAC: ""

useLayer2Config: false
mandateLayer2Config: false

openAPIValidator:
cachedFileLimit: 5
initialFilesToCache: ""
7 changes: 7 additions & 0 deletions config/config-sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,10 @@ app:

useHMACForWebhook: false
sharedKeyForWebhookHMAC: ""

useLayer2Config: false
mandateLayer2Config: false

openAPIValidator:
cachedFileLimit: 5
initialFilesToCache: ""
7 changes: 7 additions & 0 deletions config/new-config-sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,10 @@ app:

useHMACForWebhook: false
sharedKeyForWebhookHMAC: ""

useLayer2Config: false
mandateLayer2Config: false

openAPIValidator:
cachedFileLimit: 5
initialFilesToCache: ""
7 changes: 7 additions & 0 deletions config/samples/bap-client.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,10 @@ app:

useHMACForWebhook: false
sharedKeyForWebhookHMAC: ""

useLayer2Config: false
mandateLayer2Config: false

openAPIValidator:
cachedFileLimit: 5
initialFilesToCache: ""
7 changes: 7 additions & 0 deletions config/samples/bap-network.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,10 @@ app:

useHMACForWebhook: false
sharedKeyForWebhookHMAC: ""

useLayer2Config: false
mandateLayer2Config: false

openAPIValidator:
cachedFileLimit: 5
initialFilesToCache: ""
7 changes: 7 additions & 0 deletions config/samples/bpp-client.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,10 @@ app:

useHMACForWebhook: false
sharedKeyForWebhookHMAC: ""

useLayer2Config: false
mandateLayer2Config: false

openAPIValidator:
cachedFileLimit: 5
initialFilesToCache: ""
7 changes: 7 additions & 0 deletions config/samples/bpp-network.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,10 @@ app:

useHMACForWebhook: false
sharedKeyForWebhookHMAC: ""

useLayer2Config: false
mandateLayer2Config: false

openAPIValidator:
cachedFileLimit: 5
initialFilesToCache: ""
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@
"dotenv": "^16.4.1",
"express": "^4.18.1",
"express-openapi-validator": "^5.1.6",
"yaml": "^2.4.2",
"express-status-monitor": "^1.3.4",
"ioredis": "^5.0.6",
"libsodium-wrappers": "^0.7.9",
"mongodb": "^4.7.0",
"node-mocks-http": "^1.15.0",
"request-ip": "^3.3.0",
"uuid": "^8.3.2",
"winston": "^3.7.2",
"winston-daily-rotate-file": "^4.7.1",
"yaml": "^2.4.2",
"zod": "^3.14.2"
}
}
35 changes: 20 additions & 15 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ import cors from "cors";
import { Exception } from "./models/exception.model";
import {
BecknErrorDataType,
becknErrorSchema,
BecknErrorType,
} from "./schemas/becknError.schema";
import { RequestActions } from "./schemas/configs/actions.app.config.schema";
import { LookupCache } from "./utils/cache/lookup.cache.utils";
import { RequestCache } from "./utils/cache/request.cache.utils";
import { ResponseCache } from "./utils/cache/response.cache.utils";
import { SyncCache } from "./utils/cache/sync.cache.utils";
import { ClientUtils } from "./utils/client.utils";

import { getConfig } from "./utils/config.utils";
import { GatewayUtils } from "./utils/gateway.utils";
import logger from "./utils/logger.utils";
import { OpenApiValidatorMiddleware } from "./middlewares/schemaValidator.middleware";

const app = Express();

Expand All @@ -25,8 +22,16 @@ app.use(
})
);

const initializeExpress = async (successCallback: Function) => {
const initializeExpress = async () => {
const app = Express();
app.use(
require("express-status-monitor")({
path: "/process"
})
);
app.get("/status", async (req: Request, res: Response, next: NextFunction) => {
res.status(200).send('Added logic to cache OpenAPI validator spec on app load new');
});

// Enabling Cors
app.options(
Expand Down Expand Up @@ -108,7 +113,6 @@ const initializeExpress = async (successCallback: Function) => {
const PORT: number = getConfig().server.port;
app.listen(PORT, () => {
logger.info("Protocol Server started on PORT : " + PORT);
successCallback();
});
};

Expand All @@ -122,15 +126,16 @@ const main = async () => {
await LookupCache.getInstance().initialize();
await RequestCache.getInstance().initialize();

await initializeExpress(() => {
logger.info("Protocol Server Started Successfully");
logger.info("Mode: " + getConfig().app.mode.toLocaleUpperCase());
logger.info(
"Gateway Type: " +
getConfig().app.gateway.mode.toLocaleUpperCase().substring(0, 1) +
getConfig().app.gateway.mode.toLocaleUpperCase().substring(1)
);
});
await initializeExpress();
logger.info("Protocol Server Started Successfully");
logger.info("Mode: " + getConfig().app.mode.toLocaleUpperCase());
logger.info(
"Gateway Type: " +
getConfig().app.gateway.mode.toLocaleUpperCase().substring(0, 1) +
getConfig().app.gateway.mode.toLocaleUpperCase().substring(1)
);
await OpenApiValidatorMiddleware.getInstance().initOpenApiMiddleware();
logger.info('Initialized openapi validator middleware');
} catch (err) {
if (err instanceof Exception) {
logger.error(err.toString());
Expand Down
19 changes: 11 additions & 8 deletions src/controllers/bap.response.controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { NextFunction, Request, Response } from "express";
import * as AmqbLib from "amqplib";
import moment from "moment";
import { Locals } from "../interfaces/locals.interface";
import { ResponseActions } from "../schemas/configs/actions.app.config.schema";
import logger from "../utils/logger.utils";
import * as AmqbLib from "amqplib";
import { Exception, ExceptionType } from "../models/exception.model";
import { ActionUtils } from "../utils/actions.utils";
import { RequestCache } from "../utils/cache/request.cache.utils";
Expand All @@ -21,7 +22,6 @@ import {
createTelemetryEvent,
processTelemetry,
} from "../utils/telemetry.utils";
import moment from "moment";

export const bapNetworkResponseHandler = async (
req: Request,
Expand Down Expand Up @@ -67,7 +67,8 @@ export const bapNetworkResponseHandler = async (

await GatewayUtils.getInstance().sendToClientSideGateway(req.body);
console.log(
`TMTR - ${req?.body?.context?.message_id} - ${getConfig().app.mode}-${getConfig().app.gateway.mode} REV EXIT: ${new Date().valueOf()}`
`TMTR - ${req?.body?.context?.message_id} - ${req?.body?.context?.action} - ${getConfig().app.mode}-${getConfig().app.gateway.mode
} REV EXIT: ${new Date().valueOf()}`
);
} catch (err) {
let exception: Exception | null = null;
Expand All @@ -94,26 +95,28 @@ export const bapNetworkResponseSettler = async (
logger.info(
"Protocol Client Server (Network Settler) recieving message from inbox queue"
);

const responseBody = JSON.parse(message?.content.toString()!);

let responseBody = JSON.parse(message?.content.toString()!);
logger.info(
`Response from BPP NETWORK:\n ${JSON.stringify(responseBody)}\n\n`
);

const message_id = responseBody.context.message_id;
const action = ActionUtils.getCorrespondingRequestAction(
responseBody.context.action
);
console.log(
`TMTR - ${message_id} - ${getConfig().app.mode}-${getConfig().app.gateway.mode} REV ENTRY: ${new Date().valueOf()}`
`TMTR - ${message_id} - ${action} - ${getConfig().app.mode}-${getConfig().app.gateway.mode
} REV ENTRY: ${new Date().valueOf()}`
);
const unsolicitedWebhookUrl = getConfig().app.unsolicitedWebhook?.url;
const requestCache = await RequestCache.getInstance().check(
message_id,
action
);
if (!requestCache && unsolicitedWebhookUrl) {
responseBody = {
context: responseBody.context,
responses: [responseBody]
};
unsolicitedCallback(responseBody);
return;
}
Expand Down
7 changes: 4 additions & 3 deletions src/controllers/bap.trigger.controller.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { NextFunction, Request, Response } from "express";
import * as AmqbLib from "amqplib";
import { Locals } from "../interfaces/locals.interface";
import { RequestActions } from "../schemas/configs/actions.app.config.schema";
import logger from "../utils/logger.utils";
import * as AmqbLib from "amqplib";
import {
acknowledgeACK,
acknowledgeNACK
Expand All @@ -27,6 +27,7 @@ import {
createTelemetryEvent,
processTelemetry
} from "../utils/telemetry.utils";

const protocolServerLevel = `${getConfig().app.mode.toUpperCase()}-${getConfig().app.gateway.mode.toUpperCase()}`;

export const bapClientTriggerHandler = async (
Expand Down Expand Up @@ -73,7 +74,7 @@ export const bapClientTriggerHandler = async (
logger.info(`Request from client:\n ${JSON.stringify(req.body)}\n`);
await GatewayUtils.getInstance().sendToNetworkSideGateway(req.body);
console.log(
`TMTR - ${req?.body?.context?.message_id} - ${getConfig().app.mode}-${getConfig().app.gateway.mode} FORW EXIT: ${new Date().valueOf()}`
`TMTR - ${req?.body?.context?.message_id} - ${req?.body?.context?.action} - ${getConfig().app.mode}-${getConfig().app.gateway.mode} FORW EXIT: ${new Date().valueOf()}`
);
if (getConfig().client.type == ClientConfigType.synchronous) {
sendSyncResponses(
Expand Down Expand Up @@ -107,7 +108,7 @@ export const bapClientTriggerSettler = async (
try {
const body = (JSON.parse(message?.content.toString()!) as any)
console.log(
`TMTR - ${body?.context?.message_id} - ${getConfig().app.mode}-${getConfig().app.gateway.mode} FORW ENTRY: ${new Date().valueOf()}`
`TMTR - ${body?.context?.message_id} - ${body?.context?.action} - ${getConfig().app.mode}-${getConfig().app.gateway.mode} FORW ENTRY: ${new Date().valueOf()}`
);
logger.info(
"Protocol Network Server (Client Settler) recieving message from outbox queue"
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/bpp.request.controller.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { NextFunction, Request, Response } from "express";
import * as AmqbLib from "amqplib";
import moment from "moment";
import { RequestActions } from "../schemas/configs/actions.app.config.schema";
import logger from "../utils/logger.utils";
import * as AmqbLib from "amqplib";
import { Exception, ExceptionType } from "../models/exception.model";
import { acknowledgeACK } from "../utils/acknowledgement.utils";
import { GatewayUtils } from "../utils/gateway.utils";
import { RequestCache } from "../utils/cache/request.cache.utils";
import { parseRequestCache } from "../schemas/cache/request.cache.schema";
import { Locals } from "../interfaces/locals.interface";
import moment from "moment";
import { getConfig } from "../utils/config.utils";
import { ClientConfigType } from "../schemas/configs/client.config.schema";
import { requestCallback } from "../utils/callback.utils";
Expand Down Expand Up @@ -46,7 +46,7 @@ export const bppNetworkRequestHandler = async (
await processTelemetry();
}
console.log(
`TMTR - ${req.body.context?.message_id} - ${getConfig().app.mode}-${getConfig().app.gateway.mode} FORW EXIT: ${new Date().valueOf()}`
`TMTR - ${req.body.context?.message_id} - ${req?.body?.context?.action} - ${getConfig().app.mode}-${getConfig().app.gateway.mode} FORW EXIT: ${new Date().valueOf()}`
);
await GatewayUtils.getInstance().sendToClientSideGateway(req.body);
} catch (err) {
Expand All @@ -72,7 +72,7 @@ export const bppNetworkRequestSettler = async (
try {
const requestBody = JSON.parse(msg?.content.toString()!);
console.log(
`TMTR - ${requestBody?.context?.message_id} - ${getConfig().app.mode}-${getConfig().app.gateway.mode} FORW ENTRY: ${new Date().valueOf()}`
`TMTR - ${requestBody?.context?.message_id} - ${requestBody?.context?.action} - ${getConfig().app.mode}-${getConfig().app.gateway.mode} FORW ENTRY: ${new Date().valueOf()}`
);
// Generate Telemetry if enabled
if (getConfig().app.telemetry.enabled && getConfig().app.telemetry.url) {
Expand Down
9 changes: 4 additions & 5 deletions src/controllers/bpp.response.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Request, Response, NextFunction } from "express";
import * as AmqbLib from "amqplib";
import moment from "moment";
import { Locals } from "../interfaces/locals.interface";
import {
RequestActions,
ResponseActions
} from "../schemas/configs/actions.app.config.schema";
import logger from "../utils/logger.utils";
import * as AmqbLib from "amqplib";
import { Exception, ExceptionType } from "../models/exception.model";
import { GatewayUtils } from "../utils/gateway.utils";
import { RequestCache } from "../utils/cache/request.cache.utils";
Expand All @@ -28,7 +28,6 @@ import {
processTelemetry
} from "../utils/telemetry.utils";
import { GatewayMode } from "../schemas/configs/gateway.app.config.schema";
import moment from "moment";
import { getSubscriberDetails } from "../utils/lookup.utils";

export const bppClientResponseHandler = async (
Expand All @@ -41,7 +40,7 @@ export const bppClientResponseHandler = async (
acknowledgeACK(res, req.body.context);
await GatewayUtils.getInstance().sendToNetworkSideGateway(req.body);
console.log(
`TMTR - ${req?.body?.context?.message_id} - ${getConfig().app.mode}-${getConfig().app.gateway.mode} REV EXIT: ${new Date().valueOf()}`
`TMTR - ${req?.body?.context?.message_id} - ${req?.body?.context?.action} - ${getConfig().app.mode}-${getConfig().app.gateway.mode} REV EXIT: ${new Date().valueOf()}`
);
} catch (err) {
let exception: Exception | null = null;
Expand All @@ -68,7 +67,7 @@ export const bppClientResponseSettler = async (
const context = JSON.parse(JSON.stringify(responseBody.context));
const message_id = responseBody.context.message_id;
console.log(
`TMTR - ${context?.message_id} - ${getConfig().app.mode}-${getConfig().app.gateway.mode} REV ENTRY: ${new Date().valueOf()}`
`TMTR - ${context?.message_id} - ${context?.action} - ${getConfig().app.mode}-${getConfig().app.gateway.mode} REV ENTRY: ${new Date().valueOf()}`
);
const requestAction = ActionUtils.getCorrespondingRequestAction(
responseBody.context.action
Expand Down
Loading

0 comments on commit 62fa683

Please sign in to comment.