Skip to content

Commit

Permalink
Update Changes & Bug Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nixrajput committed Jul 26, 2024
1 parent ff657fc commit 8b10c61
Show file tree
Hide file tree
Showing 31 changed files with 427 additions and 246 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = {
},
plugins: ["@typescript-eslint"],
rules: {
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-explicit-any": "warn",
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/no-this-alias": "warn",
Expand Down
Binary file modified bun.lockb
Binary file not shown.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
"@types/morgan": "^1.9.9",
"@types/node": "^20.14.12",
"@types/otp-generator": "^4.0.2",
"@types/swagger-jsdoc": "^6.0.4",
"@types/swagger-ui-express": "^4.1.6",
"@typescript-eslint/eslint-plugin": "^7.17.0",
"@typescript-eslint/parser": "^7.17.0",
"eslint": "^9.7.0",
Expand All @@ -54,6 +56,8 @@
"morgan": "^1.10.0",
"node-cron": "^3.0.3",
"otp-generator": "^4.0.1",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.1",
"url": "^0.11.3",
"winston": "^3.13.1"
},
Expand Down
18 changes: 9 additions & 9 deletions src/app/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ class Database {
private _count: number = 0;

private constructor() {
Logger.info("Database :: Initializing...");
Logger.getInstance().info("Database :: Initializing...");

if (!LocalConfig.getConfig().MONGO_URI) {
Logger.error(`Database :: MongoDB URI not defined`);
Logger.getInstance().error(`Database :: MongoDB URI not defined`);
throw new Error(`Database :: MongoDB URI not defined`);
}

if (!LocalConfig.getConfig().DB_NAME) {
Logger.error(`Database :: Database name not defined`);
Logger.getInstance().error(`Database :: Database name not defined`);
throw new Error(`Database :: Database name not defined`);
}

this._isConnected = false;

Logger.info("Database :: Initialized");
Logger.getInstance().info("Database :: Initialized");
}

/**
Expand Down Expand Up @@ -52,17 +52,17 @@ class Database {
*/
public _connect = async (): Promise<void> => {
if (this._isConnected) {
Logger.info("Database :: Already Connected");
Logger.getInstance().info("Database :: Already Connected");
return;
}

const _db = this;
_db._count++;

if (_db._count > 1) {
Logger.info("Database :: Reconnecting...");
Logger.getInstance().info("Database :: Reconnecting...");
} else {
Logger.info("Database :: Connecting...");
Logger.getInstance().info("Database :: Connecting...");
}

await mongoose
Expand All @@ -73,11 +73,11 @@ class Database {
serverSelectionTimeoutMS: 5000,
})
.then(function () {
Logger.info("Database :: Connected @ MongoDB");
Logger.getInstance().info("Database :: Connected @ MongoDB");
_db._isConnected = true;
})
.catch(function (_err: mongoose.Error) {
Logger.error(`Database :: Error: ${_err.message}`);
Logger.getInstance().error(`Database :: Error: ${_err.message}`);
_db._isConnected = false;
throw _err;
});
Expand Down
120 changes: 0 additions & 120 deletions src/app/ExpressApp.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/app/Routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Routes {
*/
public mountApi(_express: Application): Application {
const apiPrefix = LocalConfig.getConfig().API_PREFIX;
Logger.info("Routes :: Mounting API routes...");
Logger.getInstance().info("Routes :: Mounting API routes...");

// Mounting Routes
_express.use(`/${apiPrefix}/auth`, AuthRouter);
Expand Down
163 changes: 146 additions & 17 deletions src/app/index.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,156 @@
import express from "express";
import type { Application } from "express";
import LocalConfig from "../configs/LocalConfig";
import Logger from "../logger";
import Database from "./Database";
import ExpressApp from "./ExpressApp";
import ExceptionHandler from "../exceptions/Handler";
import Http from "../middlewares/Http";
import CORS from "../middlewares/CORS";
import Morgan from "../middlewares/Morgan";
import Routes from "./routes";
import FirebaseConfig from "../configs/FirebaseConfig";
import SwaggerDocs from "src/configs/swagger";
import { MongoDB } from "../configs/db";

class App {
private db: Database;
/**
* @name ExpressApp
* @description Custom Express App Class Definition
*/
class ExpressApp {
public express: Application;
private _server: any;

/**
* Initializes the express server
*/
constructor() {
this.db = Database.getInstance();
Logger.getInstance().info("App :: Initializing...");

this.express = express();

this.mountLogger();
this.mountDotEnv();
this.mountMiddlewares();
this.mouteRoutes();
this.registerHandlers();
this.connectToDB();
this.mountSwagger();

Logger.getInstance().info("App :: Initialized");
}

/**
* Mount all the environmental variables
*/
private mountDotEnv(): void {
Logger.getInstance().info("Config :: Loading...");

this.express = LocalConfig.init(this.express);
this.express = FirebaseConfig.init(this.express);
}

/**
* Mount logger to the app
*/
private mountLogger(): void {
Logger._init();

Logger.getInstance().info("Logger :: Mounted");
}

/**
* Mount Swagger
*/
private mountSwagger(): void {
Logger.getInstance().info("Swagger :: Initializing...");

this.express = SwaggerDocs.init(this.express);
}

public _init = async (): Promise<void> => {
if (this.db) {
await this.db._connect();
/**
* Mounts all the defined middlewares
*/
private mountMiddlewares(): void {
Logger.getInstance().info("App :: Registering middlewares...");

if (this.db.isConnected) {
ExpressApp._init();
} else {
Logger.error("App :: Database is not connected");
}
} else {
Logger.error("App :: Database could't initialized");
// Mount basic express apis middleware
this.express = Http.mount(this.express);

// Registering Morgan Middleware
this.express = Morgan.mount(this.express);

// Check if CORS is enabled
if (LocalConfig.getConfig().CORS_ENABLED) {
// Mount CORS middleware
this.express = CORS.mount(this.express);
}
};

Logger.getInstance().info("App :: Middlewares registered");
}

/**
* Register all the handlers
*/
private registerHandlers(): void {
Logger.getInstance().info("App :: Registering handlers...");

// Registering Exception / Error Handlers
this.express.use(ExceptionHandler.logErrors);
this.express.use(ExceptionHandler.clientErrorHandler);
this.express.use(ExceptionHandler.errorHandler);
this.express = ExceptionHandler.notFoundHandler(this.express);

Logger.getInstance().info("App :: Handlers registered");
}

/**
* Mount all the routes
*/
private mouteRoutes(): void {
this.express = Routes.mountApi(this.express);
Logger.getInstance().info("Routes :: API routes mounted");
}

/**
* Connect to Database
*/
private connectToDB(): void {
Logger.getInstance().info("Database :: Connecting...");
MongoDB.getInstance().connect();
}

/**
* Starts the express server
*/
public _init(): void {
Logger.getInstance().info("Server :: Starting...");

const port = LocalConfig.getConfig().PORT || 5000;

// Start the server on the specified port
this._server = this.express
.listen(port, () => {
Logger.getInstance().info(
`Server :: Running @ 'http://localhost:${port}'`
);
Logger.getInstance().info(
`Swagger docs available at http://localhost:${port}/api-docs`
);
})
.on("error", (_error) => {
Logger.getInstance().error("Error: ", _error.message);
});
}

/**
* Close the express server
*/
public _close(): void {
Logger.getInstance().info("Server :: Stopping server...");

this._server.close(function () {
process.exit(1);
});
}
}

export default App;
export default new ExpressApp();
Loading

0 comments on commit 8b10c61

Please sign in to comment.