Updated API launch messages

This commit is contained in:
Alexis
2021-08-18 17:48:24 +02:00
parent bf0a19fa45
commit e84ff967a7
3 changed files with 47 additions and 19 deletions

View File

@@ -2,23 +2,57 @@ import express, { Application } from 'express'
import { Connection } from 'typeorm' import { Connection } from 'typeorm'
import { VSColors } from '../enums/VSColors' import { VSColors } from '../enums/VSColors'
import { AuracleApiRouter } from './AuracleApiRouter' import { AuracleApiRouter } from './AuracleApiRouter'
/**
* AuracleApi is the main class handling all the routing, modules and middlewares.
* The
* Once the constructor is called, it automatically listens on the given port.
*/
export class AuracleApi { export class AuracleApi {
/**
* Express core App.
*/
public app: Application public app: Application
public port: number
/**
* The port where the Express app is running on.
* @default 3000
*/
public port = 3000
/**
* Current version of the API.
*
* The usual format follows after `v1`
*/
public version = 'v1' public version = 'v1'
public baseUrl = `/api/${this.version}/`
/**
* Base URI to access the various endpoints of the Express App.
*
* The usual format follows
* ```javascript
* /api/${this.version}/
* ```
*/
public baseUri = `/api/${this.version}/`
constructor( constructor(
init: { init: {
port: any
database: Promise<Connection> database: Promise<Connection>
port?: any
routers?: Array<AuracleApiRouter> routers?: Array<AuracleApiRouter>
modules?: Array<any> modules?: Array<any>
middlewares?: Array<any> middlewares?: Array<any>
} }
) { ) {
this.app = express() this.app = express()
this.port = Number(init.port)
if (init.port) {
this.port = Number(init.port)
}
console.log(VSColors.cyan, `[INFO] Running in a ${process.env.NODE_ENV} environment.`)
// Making sure the DB is up before init routers, modules, etc... // Making sure the DB is up before init routers, modules, etc...
if (init.database) { if (init.database) {
@@ -43,15 +77,18 @@ export class AuracleApi {
private async connectDatabase(db_driver: Promise<Connection>) { private async connectDatabase(db_driver: Promise<Connection>) {
try { try {
// Tries to connect... // Tries to connect...
console.info(VSColors.yellow, '[TASK] Trying to connect to the database...')
const connection = await db_driver const connection = await db_driver
console.info(VSColors.cyan, '[INFO] Connection has been established successfully.') console.info(VSColors.green, '[SUCCESS] Connection has been established successfully.')
// Should the DB be rebuilt ? Are we in production ? // Should the DB be rebuilt ? Are we in production ?
const dropDatabase = (process.env.NODE_ENV === 'production') ? true : false const dropDatabase = (process.env.NODE_ENV === 'prod') ? false : true
// Then builds the data schema from registered entities... // Then builds the data schema from registered entities...
console.info(VSColors.yellow, '[TASK] Syncing database and its schemas...')
await connection.synchronize(dropDatabase) await connection.synchronize(dropDatabase)
console.info(VSColors.cyan, '[INFO] Database schemas have been generated successfully.') console.info(VSColors.green, '[SUCCESS] Syncing done.')
return connection return connection
} catch (err) { } catch (err) {
@@ -97,7 +134,7 @@ export class AuracleApi {
*/ */
private async routers(routers: Array<AuracleApiRouter>) { private async routers(routers: Array<AuracleApiRouter>) {
try { try {
routers.forEach(router => this.app.use(`${this.baseUrl}`, router.instance)) routers.forEach(router => this.app.use(`${this.baseUri}`, router.instance))
} catch (err) { } catch (err) {
console.error(VSColors.red, '[ERROR] Unable to initialize routers.') console.error(VSColors.red, '[ERROR] Unable to initialize routers.')
throw err throw err
@@ -119,6 +156,6 @@ export class AuracleApi {
* Allows the App to run on its given port * Allows the App to run on its given port
*/ */
public listen() { public listen() {
this.app.listen(this.port, () => console.info(VSColors.green, `App listening on port ${this.port} !`)) this.app.listen(this.port, () => console.info(VSColors.green, `\n[APP] App listening on http://localhost${this.baseUri}.`))
} }
} }

View File

@@ -2,10 +2,3 @@ import { createConnection } from 'typeorm';
import { dbConfig } from './config' import { dbConfig } from './config'
export const AuracleDatabaseDriver = createConnection(dbConfig); export const AuracleDatabaseDriver = createConnection(dbConfig);
/**
* If the environement is production, sync the DB
*/
if (process.env.NODE_ENV != 'production') {
// Todo
}

View File

@@ -6,8 +6,6 @@
import * as dotenv from 'dotenv' import * as dotenv from 'dotenv'
dotenv.config(); dotenv.config();
// Packages
// TypeORM // TypeORM
import "reflect-metadata"; import "reflect-metadata";
@@ -23,8 +21,8 @@ import { SpellRouter } from './routes/SpellRouter';
const apiPort = process.env.API_PORT const apiPort = process.env.API_PORT
const api = new AuracleApi({ const api = new AuracleApi({
port: apiPort,
database: AuracleDatabaseDriver, database: AuracleDatabaseDriver,
port: apiPort,
middlewares: [], middlewares: [],
routers: [ routers: [