From 698928847181fc5ad0073696ebe34dd017daaad4 Mon Sep 17 00:00:00 2001 From: Kfir Dayan Date: Sun, 9 Jul 2023 00:28:31 +0300 Subject: [PATCH] env vars from singleton service class --- .../src/controllers/todoController.ts | 3 --- todo-service/src/main.ts | 10 ++++---- .../src/middleware/createTodoMiddleWare.ts | 1 - todo-service/src/rabbitmq/RabbitMQ.ts | 15 +++++------ todo-service/src/services/EnvService.ts | 25 +++++++++++++++++++ 5 files changed, 38 insertions(+), 16 deletions(-) create mode 100644 todo-service/src/services/EnvService.ts diff --git a/todo-service/src/controllers/todoController.ts b/todo-service/src/controllers/todoController.ts index 6744bab..8dc8dab 100644 --- a/todo-service/src/controllers/todoController.ts +++ b/todo-service/src/controllers/todoController.ts @@ -2,11 +2,8 @@ import { NextFunction, Request, Response } from "express"; import { ApiError } from "../utils/ApiError"; import { ITodo } from "../schemas/todoSchema"; import { TodoModel } from "../models/todoModel"; -// import { Sqs } from '../aws/Sqs'; import { RabbitMQ } from "../rabbitmq/RabbitMQ"; -const env = require("dotenv").config().parsed; - export class TodoController { private todoModel: TodoModel; queue: RabbitMQ; diff --git a/todo-service/src/main.ts b/todo-service/src/main.ts index e357fde..17ff8ef 100644 --- a/todo-service/src/main.ts +++ b/todo-service/src/main.ts @@ -1,15 +1,15 @@ import express from "express"; import mongoose from "mongoose"; import todoRouter from "./routes/todoRouter"; - +import { EnvService } from "./services/EnvService"; import { ApiError } from "./utils/ApiError"; -const env = require("dotenv").config().parsed; - class TodoApp { app: express.Application; + envService: EnvService; constructor() { + this.envService = EnvService.getInstance(); this.connectToDB(); } @@ -29,7 +29,7 @@ class TodoApp { } private startServer() { - const PORT = env.PORT || 3000; + const PORT = this.envService.getEnvVariable("PORT") || 3000; this.app.listen(PORT, () => { console.log(`Server started on port ${PORT}`); }); @@ -40,7 +40,7 @@ class TodoApp { } private connectToDB() { - mongoose.connect(env.DATABASE_URL); + mongoose.connect(this.envService.getEnvVariable("DATABASE_URL")); const db = mongoose.connection; // Check for DB connection db.on("error", () => { diff --git a/todo-service/src/middleware/createTodoMiddleWare.ts b/todo-service/src/middleware/createTodoMiddleWare.ts index fd733cb..d48bb3c 100644 --- a/todo-service/src/middleware/createTodoMiddleWare.ts +++ b/todo-service/src/middleware/createTodoMiddleWare.ts @@ -1,7 +1,6 @@ import { Request, Response, NextFunction } from "express"; import { ApiError } from "../utils/ApiError"; import { DateTime } from "luxon"; -import moment from "moment-timezone"; const createTodoMiddleWare = async ( req: Request, diff --git a/todo-service/src/rabbitmq/RabbitMQ.ts b/todo-service/src/rabbitmq/RabbitMQ.ts index 0333ea0..9ce035b 100644 --- a/todo-service/src/rabbitmq/RabbitMQ.ts +++ b/todo-service/src/rabbitmq/RabbitMQ.ts @@ -1,16 +1,17 @@ import amqp, { Options, ConsumeMessage, Channel } from "amqplib"; import { ITodo } from "../schemas/todoSchema"; - -const env = require("dotenv").config().parsed; +import { EnvService } from "../services/EnvService"; export class RabbitMQ { connection: amqp.Connection; channel: amqp.Channel; queue: string; exchange: string; + envService: EnvService; constructor() { - this.queue = env.RABBITMQ_QUEUE_NAME; + this.envService = EnvService.getInstance(); + this.queue = this.envService.getEnvVariable("RABBITMQ_QUEUE_NAME"); this.exchange = "delayed_exchange"; this.connect() @@ -26,10 +27,10 @@ export class RabbitMQ { try { this.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 this.connection.createChannel(); diff --git a/todo-service/src/services/EnvService.ts b/todo-service/src/services/EnvService.ts new file mode 100644 index 0000000..5c7ccea --- /dev/null +++ b/todo-service/src/services/EnvService.ts @@ -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]; + } +}