Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: rabbitmq, nats and rest as relay services #127

Merged
merged 8 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 177 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tazama-lf/frms-coe-startup-lib",
"version": "2.3.0-rc.5",
"version": "2.3.0-rc.6",
"description": "FRMS Center of Excellence startup package library",
"main": "lib/index.js",
"repository": {
Expand Down Expand Up @@ -29,6 +29,9 @@
},
"dependencies": {
"@tazama-lf/frms-coe-lib": "^5.0.0-rc.4",
"@types/amqplib": "^0.10.5",
cshezi marked this conversation as resolved.
Show resolved Hide resolved
"amqplib": "^0.10.4",
"axios": "^1.7.7",
"nats": "^2.28.2"
},
"keywords": [],
Expand All @@ -37,7 +40,7 @@
"devDependencies": {
"@stylistic/eslint-plugin": "^2.1.0",
"@types/jest": "^29.5.1",
"@types/node": "^20.14.2",
"@types/node": "^20.17.6",
"@typescript-eslint/eslint-plugin": "^7.12.0",
"@typescript-eslint/parser": "^7.12.0",
"dotenv": "^16.1.2",
Expand Down
16 changes: 16 additions & 0 deletions src/interfaces/iRelayConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: Apache-2.0

import { validateEnvVar } from '@tazama-lf/frms-coe-lib/lib/config';
import { startupConfig } from './iStartupConfig';

export interface IRelayConfig {
destinationType: 'nats' | 'rabbitmq' | 'rest';
destinationUrl: string;
producerStream: string;
}

export const relayConfig: IRelayConfig = {
destinationType: validateEnvVar('DESTINATION_TYPE', 'string'),
destinationUrl: validateEnvVar('DESTINATION_URL', 'string'),
producerStream: startupConfig.producerStreamName,
};
3 changes: 3 additions & 0 deletions src/interfaces/iRelayService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface IRelay {
relay: (data: Uint8Array) => Promise<void>;
}
31 changes: 31 additions & 0 deletions src/services/natsRelayService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { connect, type NatsConnection } from 'nats';
import { relayConfig } from '../interfaces/iRelayConfig';
import { type IRelay } from '../interfaces/iRelayService';
import { type ILoggerService } from '../interfaces';
import { startupConfig } from '../interfaces/iStartupConfig';

export class NatsRelay implements IRelay {
private readonly config = relayConfig;
private NatsConn_Producer?: NatsConnection;
private logger?: ILoggerService | Console;

async init(loggerService?: ILoggerService): Promise<void> {
if (loggerService) {
cshezi marked this conversation as resolved.
Show resolved Hide resolved
this.logger = startupConfig.env === 'dev' || startupConfig.env === 'test' ? console : loggerService;
} else {
this.logger = console;
}

this.NatsConn_Producer = await connect({
servers: this.config.destinationUrl,
});
this.logger.log(`[TRS]: Connected to Client NATS: ${JSON.stringify(this.NatsConn_Producer.info, null, 4)}`);
}
async relay(data: Uint8Array): Promise<void> {
try {
this.NatsConn_Producer?.publish(relayConfig.producerStream, data);
} catch (error) {
this.logger?.error(`[TRS]: Connected to Client NATS: ${JSON.stringify(this.NatsConn_Producer?.info, null, 4)}`);
}
}
}
Loading
Loading