From 633673ec3e31e9e29dfef1fd3b213a32e95d5865 Mon Sep 17 00:00:00 2001 From: Vinayak Sarawagi Date: Sun, 17 Nov 2024 23:20:05 +0530 Subject: [PATCH] refactor(core): console related cleanups --- integrations/sample-app/config/app.ts | 9 +++++ packages/core/lib/database/service.ts | 14 ------- packages/core/lib/interfaces/config.ts | 1 + packages/core/lib/rest/foundation/server.ts | 43 +++++++++++++++++---- packages/core/lib/storage/service.ts | 1 - packages/core/lib/utils/console-helpers.ts | 9 +++++ 6 files changed, 54 insertions(+), 23 deletions(-) diff --git a/integrations/sample-app/config/app.ts b/integrations/sample-app/config/app.ts index 8d0b1d4..f83ffa3 100644 --- a/integrations/sample-app/config/app.ts +++ b/integrations/sample-app/config/app.ts @@ -49,6 +49,15 @@ export default registerNamespace( */ url: process.env.APP_URL || 'localhost', + /** + * ----------------------------------------------------- + * Hostname + * ----------------------------------------------------- + * + * This address to bind the HTTP Listener to. + */ + hostname: process.env.HOSTNAME || '0.0.0.0', + /** * ----------------------------------------------------- * Application Port diff --git a/packages/core/lib/database/service.ts b/packages/core/lib/database/service.ts index 5f2d310..2123275 100644 --- a/packages/core/lib/database/service.ts +++ b/packages/core/lib/database/service.ts @@ -1,7 +1,5 @@ import { Injectable, OnModuleInit } from '@nestjs/common'; import Knex, { Knex as KnexType } from 'knex'; -import { logTime } from '../utils/helpers'; -import { InternalLogger } from '../utils/logger'; import { BaseModel } from './baseModel'; import { ConnectionNotFound } from './exceptions'; import { DatabaseOptions, DbConnectionOptions } from './options'; @@ -33,20 +31,8 @@ export class ObjectionService implements OnModuleInit { try { await connection.raw(dbOptions.validateQuery || 'select 1+1 as result'); - InternalLogger.success( - 'DatabaseService', - `Was able to successfully validate [${connName}] connection ${logTime( - Date.now() - time, - )}`, - ); } catch (_e) { const e = _e as Error; - InternalLogger.error( - 'DatabaseService', - `Validation for connection [${connName}] failed with reason ${ - e.message - } ${logTime(Date.now() - time)}`, - ); } } } diff --git a/packages/core/lib/interfaces/config.ts b/packages/core/lib/interfaces/config.ts index 3682bf5..30c419d 100644 --- a/packages/core/lib/interfaces/config.ts +++ b/packages/core/lib/interfaces/config.ts @@ -15,6 +15,7 @@ export interface AppConfig { env: string; debug: boolean; url: string; + hostname?: string; port: number; cors: CorsOptions | CorsOptionsDelegate; error?: { diff --git a/packages/core/lib/rest/foundation/server.ts b/packages/core/lib/rest/foundation/server.ts index b95954e..2d97e2d 100644 --- a/packages/core/lib/rest/foundation/server.ts +++ b/packages/core/lib/rest/foundation/server.ts @@ -1,7 +1,6 @@ import { HttpAdapterHost, NestFactory } from '@nestjs/core'; import { NestExpressApplication } from '@nestjs/platform-express'; import { useContainer } from 'class-validator'; -// import 'console.mute'; import { ConfigService } from '../../config/service'; import { IntentExceptionFilter } from '../../exceptions'; import { IntentAppContainer, ModuleBuilder } from '../../foundation'; @@ -9,6 +8,9 @@ import { Type } from '../../interfaces'; import { Obj, Package } from '../../utils'; import { Kernel } from '../foundation/kernel'; import { requestMiddleware } from '../middlewares/functional/requestSerializer'; +import pc from 'picocolors'; +import { printBulletPoints } from '../../utils/console-helpers'; +import 'console.mute'; export class IntentHttpServer { private kernel: Kernel; @@ -36,11 +38,11 @@ export class IntentHttpServer { } async start() { + console['mute'](); const module = ModuleBuilder.build(this.container, this.kernel); - - // console['mute'](); const app = await NestFactory.create(module, { bodyParser: true, + logger: false, }); if (this.errorHandler) { @@ -52,8 +54,6 @@ export class IntentHttpServer { app.useBodyParser('raw'); app.useBodyParser('urlencoded'); - // console['resume'](); - app.use(requestMiddleware); useContainer(app.select(module), { fallbackOnErrors: true }); @@ -64,9 +64,36 @@ export class IntentHttpServer { this.configureErrorReporter(config.get('app.sentry')); - // options?.globalPrefix && app.setGlobalPrefix(options.globalPrefix); - console.log('server listening on ===> '); - await app.listen((config.get('app.port') as number) || 5001); + const port = config.get('app.port'); + const hostname = config.get('app.hostname'); + const environment = config.get('app.env'); + const debug = config.get('app.debug'); + + await app.listen(+port || 5001, hostname); + + console['resume'](); + + console.clear(); + + console.log(` ${pc.green(pc.bold('Intent'))} ${pc.green('v1.5.0')}`); + console.log(); + + printBulletPoints([ + ['➜', 'environment', environment], + ['➜', 'debug', debug], + ['➜', 'hostname', hostname], + ['➜', 'port', port], + ]); + + const url = new URL( + ['127.0.0.1', '0.0.0.0'].includes(hostname) + ? 'http://localhost' + : `http://${hostname}`, + ); + url.port = port; + + console.log(); + console.log(` ${pc.white('Listening on')}: ${pc.cyan(url.toString())}`); } configureErrorReporter(config: Record) { diff --git a/packages/core/lib/storage/service.ts b/packages/core/lib/storage/service.ts index c0341d1..93b36db 100644 --- a/packages/core/lib/storage/service.ts +++ b/packages/core/lib/storage/service.ts @@ -1,7 +1,6 @@ import { Injectable } from '@nestjs/common'; import { LocalDiskOptions, S3DiskOptions } from './interfaces'; import { StorageDriver } from './interfaces'; -import { InternalLogger } from '../utils/logger'; import { DiskNotFoundException } from './exceptions/diskNotFound'; import { ConfigService } from '../config'; import { DriverMap } from './driver-mapper'; diff --git a/packages/core/lib/utils/console-helpers.ts b/packages/core/lib/utils/console-helpers.ts index c38c7d0..5bf88cf 100644 --- a/packages/core/lib/utils/console-helpers.ts +++ b/packages/core/lib/utils/console-helpers.ts @@ -44,3 +44,12 @@ export const jsonToArchy = (obj: Record, key = '') => { return { label, nodes }; }; + +export const printBulletPoints = (rows: string[][]) => { + for (const row of rows) { + const [style, attribute, value] = row; + console.log( + ` ${pc.green(pc.dim(style))} ${pc.white(attribute)}: ${pc.yellow(value)}`, + ); + } +};