env vars from singleton service class
This commit is contained in:
parent
6989288471
commit
74f6fdcf0f
3 changed files with 39 additions and 39 deletions
|
@ -1,19 +1,20 @@
|
|||
import { MongoClient, ObjectId } from "mongodb";
|
||||
import { ITodo } from "../interfaces/ITodo";
|
||||
|
||||
const env = require("dotenv").config().parsed;
|
||||
import { EnvService } from "../services/EnvService";
|
||||
|
||||
export class MongoDbModel {
|
||||
client: MongoClient;
|
||||
envSerivce: EnvService;
|
||||
|
||||
constructor() {
|
||||
this.client = new MongoClient(env.DATABASE_URL);
|
||||
this.envSerivce = EnvService.getInstance();
|
||||
this.client = new MongoClient(this.envSerivce.getEnvVariable('DATABASE_URL'));
|
||||
}
|
||||
|
||||
public updateTodoStatus = async (todo: ITodo) => {
|
||||
try {
|
||||
await this.client.connect();
|
||||
const db = this.client.db(env.MONGO_DB_NAME);
|
||||
const db = this.client.db(this.envSerivce.getEnvVariable('MONGO_DB_NAME'));
|
||||
const todosCollection = db.collection("todos");
|
||||
const result = await todosCollection.updateOne(
|
||||
{ _id: new ObjectId(todo._id) },
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
import amqp, { ConsumeMessage } from "amqplib";
|
||||
import { ITodo } from "../interfaces/ITodo";
|
||||
import { MongoDbModel } from "../mongodb/MongoDb";
|
||||
|
||||
const env = require("dotenv").config().parsed;
|
||||
import { EnvService } from "../services/EnvService";
|
||||
|
||||
export class RabbitMQ {
|
||||
channel: amqp.Channel;
|
||||
|
@ -10,8 +9,11 @@ export class RabbitMQ {
|
|||
exchangeName: string;
|
||||
|
||||
mongoClient: MongoDbModel;
|
||||
envService: EnvService;
|
||||
|
||||
constructor() {
|
||||
this.queueName = env.RABBITMQ_QUEUE_NAME;
|
||||
this.envService = EnvService.getInstance();
|
||||
this.queueName = this.envService.getEnvVariable('RABBITMQ_QUEUE_NAME');
|
||||
|
||||
this.mongoClient = new MongoDbModel();
|
||||
|
||||
|
@ -28,10 +30,10 @@ export class RabbitMQ {
|
|||
try {
|
||||
const connection = await amqp.connect({
|
||||
protocol: "amqp",
|
||||
hostname: env.RABBITMQ_HOST,
|
||||
port: parseInt(env.RABBITMQ_PORT),
|
||||
username: env.RABBITMQ_USERNAME,
|
||||
password: env.RABBITMQ_PASSWORD,
|
||||
hostname: this.envService.getEnvVariable('RABBITMQ_HOST'),
|
||||
port: parseInt(this.envService.getEnvVariable('RABBITMQ_PORT')),
|
||||
username: this.envService.getEnvVariable('RABBITMQ_USERNAME'),
|
||||
password: this.envService.getEnvVariable('RABBITMQ_PASSWORD'),
|
||||
});
|
||||
|
||||
this.channel = await connection.createChannel();
|
||||
|
@ -74,32 +76,4 @@ export class RabbitMQ {
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
// try {
|
||||
// console.log('Consumer started, waiting for messages...');
|
||||
// this.channel.consume(this.queueName, async (message) => {
|
||||
// if (message) {
|
||||
// const { payload, delayTimeForQueue } = JSON.parse(message.content.toString()) as {
|
||||
// payload: ITodo;
|
||||
// delayTimeForQueue: number;
|
||||
// };
|
||||
|
||||
// console.log('Received notification:', payload);
|
||||
|
||||
// try {
|
||||
// await this.mongoClient.updateTodoStatus(payload);
|
||||
// console.log('Updated todo status in the DB');
|
||||
// } catch {
|
||||
// await this.create(payload, delayTimeForQueue);
|
||||
// this.channel.ack(message);
|
||||
// console.log('Published new message with delay, THE DB IS DOWN!: ', delayTimeForQueue);
|
||||
// return;
|
||||
// }
|
||||
|
||||
// this.channel.ack(message);
|
||||
// }
|
||||
// });
|
||||
// } catch (error) {
|
||||
// console.error('Error consuming messages from RabbitMQ:', error);
|
||||
// }
|
||||
}
|
||||
|
|
25
notification-service/src/services/EnvService.ts
Normal file
25
notification-service/src/services/EnvService.ts
Normal file
|
@ -0,0 +1,25 @@
|
|||
import dotenv from "dotenv";
|
||||
|
||||
// Load environment variables from .env file
|
||||
dotenv.config();
|
||||
|
||||
export class EnvService {
|
||||
private static instance: EnvService;
|
||||
private env: any;
|
||||
|
||||
private constructor() {
|
||||
// Get the parsed environment variables
|
||||
this.env = dotenv.config().parsed;
|
||||
}
|
||||
|
||||
public static getInstance(): EnvService {
|
||||
if (!EnvService.instance) {
|
||||
EnvService.instance = new EnvService();
|
||||
}
|
||||
return EnvService.instance;
|
||||
}
|
||||
|
||||
public getEnvVariable(name: string): string | undefined {
|
||||
return this.env[name];
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue