Skip to content

Commit

Permalink
refactor config.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
jipsonminibhavan committed Feb 5, 2024
1 parent 4278b55 commit 5c1ce16
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
31 changes: 23 additions & 8 deletions backend/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import dotenv from "dotenv";

dotenv.config({ path: "./env.prod" });

export const PORT: number = parseInt(process.env.PORT || "8080", 10);
export const MONGODB_URI: string =
process.env.MONGODB_URI || "mongodb://localhost:27017/suriya";
export const API_VERSION: string = process.env.API_VERSION || "1.5.5";
export const PASSPORT_SECRET: string = "nodeauthsecret";
export const config = {
dotenv.config({});

const baseConfig = {
PORT: parseInt(process.env.PORT || "8080", 10),
API_VERSION: process.env.API_VERSION || "1.5.5",
PASSPORT_SECRET: "nodeauthsecret",
jwtSecret: "JWT Secret",
jwtSession: { session: false },
};
let envConfig;
switch (process.env.PROD_ENV) {
case "prod":
envConfig = {
MONGODB_URI: process.env.MONGODB_URI_PROD,
};
break;
default:
envConfig = {
MONGODB_URI:
process.env.MONGODB_URI || "mongodb://localhost:27017/suriya",
};
}

const config = { ...baseConfig, ...envConfig };

export default config;
6 changes: 3 additions & 3 deletions backend/src/db.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import mongoose from "mongoose";
import { PORT, MONGODB_URI, API_VERSION } from "./config";
import config from "./config";
import { Status } from "./models/Status";

const connectToDB = async () => {
try {
const instance = await mongoose.connect(MONGODB_URI);
const instance = await mongoose.connect(config.MONGODB_URI);
console.log("Connected to MongoDB");

const status = new Status({ db: true, version: "1.0.0" });
const status = new Status({ db: true, version: config.API_VERSION });
await Status.createCollection();

const session = await instance.startSession();
Expand Down
8 changes: 4 additions & 4 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import express, { Request, Response } from "express";
import bodyParser from "body-parser";
import cors from "cors";
import connectToDB from "./db";
import { PORT, PASSPORT_SECRET } from "./config";
import config from "./config";
import passport from "passport";
import { Strategy as LocalStrategy } from "passport-local";
import { UserModel } from "./models/Users";
Expand Down Expand Up @@ -31,7 +31,7 @@ app.get("/corstest", (req: Request, res: Response) => {

app.use(
session({
secret: PASSPORT_SECRET,
secret: config.PASSPORT_SECRET,
resave: false,
saveUninitialized: false,
})
Expand All @@ -48,6 +48,6 @@ app.use("/", statusRoutes);
app.use("/", linkRoutes);

//Express-Server
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
app.listen(config.PORT, () => {
console.log(`Server is running on port ${config.PORT}`);
});
2 changes: 1 addition & 1 deletion backend/src/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from "passport-jwt";
import passport from "passport";
import { UserModel, User } from "../models/Users";
import { config } from "../config";
import config from "../config";

const jwtOptions: StrategyOptions = {
//Authorization: Bearer in request headers
Expand Down

0 comments on commit 5c1ce16

Please sign in to comment.