From fabe285fb22861a86a1a23a043258ca419021145 Mon Sep 17 00:00:00 2001 From: Alexis <35.alexis.pele@gmail.com> Date: Tue, 3 Aug 2021 17:33:20 +0200 Subject: [PATCH] Rearranged and refactored main Api Class --- src/common/classes/AuracleApi.ts | 88 +++++++++++++++++--------------- src/main.ts | 5 +- 2 files changed, 49 insertions(+), 44 deletions(-) diff --git a/src/common/classes/AuracleApi.ts b/src/common/classes/AuracleApi.ts index 389cecd..7da6959 100644 --- a/src/common/classes/AuracleApi.ts +++ b/src/common/classes/AuracleApi.ts @@ -1,5 +1,6 @@ import express, { Application } from 'express' import { Connection } from 'typeorm' +import { VSColors } from '../enums/VSColors' import { AuracleApiRouter } from './AuracleApiRouter' export class AuracleApi { public app: Application @@ -21,24 +22,17 @@ export class AuracleApi { // Making sure the DB is up before init routers, modules, etc... if (init.database) { - this.db_connect(init.database) - .then(() => { + this.connectDatabase(init.database) + .then(async () => { - // Sets up tasks to execute... - const tasks = [ - this.modules(init.modules), - this.routers(init.routers), - this.middlewares(init.middlewares) - ] + // Loads other services + await this.registerServices(init.routers, init.modules, init.middlewares) - Promise.all(tasks) - .catch((err) => { - throw err - }) + this.listen() }) .catch((err) => { - console.error("App couldn't boot up with the following errors : ") - console.error(err) + console.error(VSColors.red, "[ERROR] App couldn't boot up with the following errors : ") + console.error(VSColors.red, err) }) } } @@ -46,33 +40,53 @@ export class AuracleApi { /** * Links the API with a TypeORM connection */ - private db_connect = async (db_driver: Promise) => { + private async connectDatabase(db_driver: Promise) { try { + // Tries to connect... const connection = await db_driver - console.info('Connection has been established successfully.') + console.info(VSColors.cyan, '[INFO] Connection has been established successfully.') - // Builds the data schema from registered entities - await connection.synchronize() - console.info('Database schemas have been generated successfully.') + // Should the DB be rebuilt ? Are we in production ? + const dropDatabase = (process.env.NODE_ENV === 'production') ? true : false + + // Then builds the data schema from registered entities... + await connection.synchronize(dropDatabase) + console.info(VSColors.cyan, '[INFO] Database schemas have been generated successfully.') return connection } catch (err) { - console.error('Unable to connect to the database.') + console.error(VSColors.red, '[ERROR] Unable to connect to the database !') throw err } } + /** + * Registers other express services to add onto the core app + * + * @param routers A list of Express Routers extended from AuracleApiRouter + * @param modules A list of Express extensions + * @param middlewares A list of Express middlewares that handle other functions + */ + private async registerServices(routers: Array, modules: Array, middlewares: Array) { + return Promise.all([ + this.modules(modules), + this.routers(routers), + this.middlewares(middlewares) + ]) + .catch((err) => { + throw err + }) + } + /** * Initializes the App with a bunch of modules like helmet, cors, morgan... * @param modules An array of modules and extensions */ - private modules = async (modules: Array) => { + private async modules(modules: Array) { try { - modules.forEach((module) => { - this.app.use(module) - }) + modules.forEach(module => this.app.use(module)) } catch (err) { - console.error('Unable to initialize modules and extensions.') + console.error(VSColors.red, '[ERROR] Unable to initialize modules and extensions.') throw err } } @@ -81,26 +95,22 @@ export class AuracleApi { * Initializes routers * @param routers An array of AuracleApiRouter children objects */ - private routers = async (routers: Array) => { + private async routers(routers: Array) { try { - routers.forEach((router) => { - this.app.use(`${this.baseUrl}`, router.instance) - }) + routers.forEach(router => this.app.use(`${this.baseUrl}`, router.instance)) } catch (err) { - console.error('Unable to initialize routers.') + console.error(VSColors.red, '[ERROR] Unable to initialize routers.') throw err } } - private middlewares = async (middleWares: { + private async middlewares(middleWares: { forEach: (arg0: (middleWare: any) => void) => void; - }) => { + }) { try { - middleWares.forEach((middleWare) => { - this.app.use(middleWare) - }) + middleWares.forEach(middleWare => this.app.use(middleWare)) } catch (err) { - console.error('Unable to initialize middlewares.') + console.error(VSColors.red, '[ERROR] Unable to initialize middlewares.') throw err } } @@ -108,9 +118,7 @@ export class AuracleApi { /** * Allows the App to run on its given port */ - public listen = () => { - this.app.listen(this.port, () => { - console.log(`App listening on port ${this.port}`) - }) + public listen() { + this.app.listen(this.port, () => console.info(VSColors.green, `App listening on port ${this.port} !`)) } } diff --git a/src/main.ts b/src/main.ts index 61ba26f..e226997 100644 --- a/src/main.ts +++ b/src/main.ts @@ -22,7 +22,7 @@ import { SpellRouter } from './routes/SpellRouter'; const apiPort = process.env.API_PORT -const app = new AuracleApi({ +const api = new AuracleApi({ port: apiPort, database: AuracleDatabaseDriver, @@ -36,6 +36,3 @@ const app = new AuracleApi({ helmet() ], }) - -// Listens on apiPort... -app.listen()