env vars from singleton service class
This commit is contained in:
parent
3e91c159d7
commit
6989288471
5 changed files with 38 additions and 16 deletions
|
@ -2,11 +2,8 @@ import { NextFunction, Request, Response } from "express";
|
||||||
import { ApiError } from "../utils/ApiError";
|
import { ApiError } from "../utils/ApiError";
|
||||||
import { ITodo } from "../schemas/todoSchema";
|
import { ITodo } from "../schemas/todoSchema";
|
||||||
import { TodoModel } from "../models/todoModel";
|
import { TodoModel } from "../models/todoModel";
|
||||||
// import { Sqs } from '../aws/Sqs';
|
|
||||||
import { RabbitMQ } from "../rabbitmq/RabbitMQ";
|
import { RabbitMQ } from "../rabbitmq/RabbitMQ";
|
||||||
|
|
||||||
const env = require("dotenv").config().parsed;
|
|
||||||
|
|
||||||
export class TodoController {
|
export class TodoController {
|
||||||
private todoModel: TodoModel;
|
private todoModel: TodoModel;
|
||||||
queue: RabbitMQ;
|
queue: RabbitMQ;
|
||||||
|
|
|
@ -1,15 +1,15 @@
|
||||||
import express from "express";
|
import express from "express";
|
||||||
import mongoose from "mongoose";
|
import mongoose from "mongoose";
|
||||||
import todoRouter from "./routes/todoRouter";
|
import todoRouter from "./routes/todoRouter";
|
||||||
|
import { EnvService } from "./services/EnvService";
|
||||||
import { ApiError } from "./utils/ApiError";
|
import { ApiError } from "./utils/ApiError";
|
||||||
|
|
||||||
const env = require("dotenv").config().parsed;
|
|
||||||
|
|
||||||
class TodoApp {
|
class TodoApp {
|
||||||
app: express.Application;
|
app: express.Application;
|
||||||
|
envService: EnvService;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
|
this.envService = EnvService.getInstance();
|
||||||
this.connectToDB();
|
this.connectToDB();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ class TodoApp {
|
||||||
}
|
}
|
||||||
|
|
||||||
private startServer() {
|
private startServer() {
|
||||||
const PORT = env.PORT || 3000;
|
const PORT = this.envService.getEnvVariable("PORT") || 3000;
|
||||||
this.app.listen(PORT, () => {
|
this.app.listen(PORT, () => {
|
||||||
console.log(`Server started on port ${PORT}`);
|
console.log(`Server started on port ${PORT}`);
|
||||||
});
|
});
|
||||||
|
@ -40,7 +40,7 @@ class TodoApp {
|
||||||
}
|
}
|
||||||
|
|
||||||
private connectToDB() {
|
private connectToDB() {
|
||||||
mongoose.connect(env.DATABASE_URL);
|
mongoose.connect(this.envService.getEnvVariable("DATABASE_URL"));
|
||||||
const db = mongoose.connection;
|
const db = mongoose.connection;
|
||||||
// Check for DB connection
|
// Check for DB connection
|
||||||
db.on("error", () => {
|
db.on("error", () => {
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, NextFunction } from "express";
|
||||||
import { ApiError } from "../utils/ApiError";
|
import { ApiError } from "../utils/ApiError";
|
||||||
import { DateTime } from "luxon";
|
import { DateTime } from "luxon";
|
||||||
import moment from "moment-timezone";
|
|
||||||
|
|
||||||
const createTodoMiddleWare = async (
|
const createTodoMiddleWare = async (
|
||||||
req: Request,
|
req: Request,
|
||||||
|
|
|
@ -1,16 +1,17 @@
|
||||||
import amqp, { Options, ConsumeMessage, Channel } from "amqplib";
|
import amqp, { Options, ConsumeMessage, Channel } from "amqplib";
|
||||||
import { ITodo } from "../schemas/todoSchema";
|
import { ITodo } from "../schemas/todoSchema";
|
||||||
|
import { EnvService } from "../services/EnvService";
|
||||||
const env = require("dotenv").config().parsed;
|
|
||||||
|
|
||||||
export class RabbitMQ {
|
export class RabbitMQ {
|
||||||
connection: amqp.Connection;
|
connection: amqp.Connection;
|
||||||
channel: amqp.Channel;
|
channel: amqp.Channel;
|
||||||
queue: string;
|
queue: string;
|
||||||
exchange: string;
|
exchange: string;
|
||||||
|
envService: EnvService;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.queue = env.RABBITMQ_QUEUE_NAME;
|
this.envService = EnvService.getInstance();
|
||||||
|
this.queue = this.envService.getEnvVariable("RABBITMQ_QUEUE_NAME");
|
||||||
this.exchange = "delayed_exchange";
|
this.exchange = "delayed_exchange";
|
||||||
|
|
||||||
this.connect()
|
this.connect()
|
||||||
|
@ -26,10 +27,10 @@ export class RabbitMQ {
|
||||||
try {
|
try {
|
||||||
this.connection = await amqp.connect({
|
this.connection = await amqp.connect({
|
||||||
protocol: "amqp",
|
protocol: "amqp",
|
||||||
hostname: env.RABBITMQ_HOST,
|
hostname: this.envService.getEnvVariable("RABBITMQ_HOST"),
|
||||||
port: parseInt(env.RABBITMQ_PORT),
|
port: parseInt(this.envService.getEnvVariable('RABBITMQ_PORT')),
|
||||||
username: env.RABBITMQ_USERNAME,
|
username: this.envService.getEnvVariable('RABBITMQ_USERNAME'),
|
||||||
password: env.RABBITMQ_PASSWORD,
|
password: this.envService.getEnvVariable('RABBITMQ_PASSWORD'),
|
||||||
});
|
});
|
||||||
|
|
||||||
this.channel = await this.connection.createChannel();
|
this.channel = await this.connection.createChannel();
|
||||||
|
|
25
todo-service/src/services/EnvService.ts
Normal file
25
todo-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