env vars from singleton service class

This commit is contained in:
Kfir Dayan 2023-07-09 00:28:31 +03:00
parent 3e91c159d7
commit 6989288471
5 changed files with 38 additions and 16 deletions

View file

@ -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;

View file

@ -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", () => {

View file

@ -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,

View file

@ -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();

View 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];
}
}