From 6f56372dc32e5bb1babb26fb3b5a193237d334cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lenon?= Date: Wed, 6 Apr 2022 10:05:07 -0300 Subject: [PATCH] fix: fetch global middlewares inside the ioc --- package-lock.json | 18 +++++++++--------- package.json | 4 ++-- src/Application.ts | 32 ++++++++++++++++++++++---------- src/Ignite.ts | 6 ++++-- tests/Stubs/config/logging.ts | 30 ++++++++++++++++++++---------- 5 files changed, 57 insertions(+), 33 deletions(-) diff --git a/package-lock.json b/package-lock.json index b1b740e..96a7d28 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,18 +1,18 @@ { "name": "@athenna/core", - "version": "1.1.3", + "version": "1.1.5", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@athenna/core", - "version": "1.1.3", + "version": "1.1.5", "license": "MIT", "dependencies": { "@athenna/config": "1.0.5", "@athenna/http": "1.1.3", "@athenna/ioc": "1.0.9", - "@athenna/logger": "1.0.9", + "@athenna/logger": "1.1.1", "@secjs/utils": "1.8.0", "reflect-metadata": "0.1.13", "tscpaths": "0.0.9" @@ -201,9 +201,9 @@ } }, "node_modules/@athenna/logger": { - "version": "1.0.9", - "resolved": "https://registry.npmjs.org/@athenna/logger/-/logger-1.0.9.tgz", - "integrity": "sha512-qJa0VSeKWjLrZ9i0roa3MC2abem8whYA0i2XPHbUZ0WP7mvwCRT2EJZZex1Jv4nB+wLZy4M6f5bFmwOwztksXQ==", + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@athenna/logger/-/logger-1.1.1.tgz", + "integrity": "sha512-cHv846NbsYVPeIhEXUsiGch48sfVLtMNXbK2eFVl7JV32TYP56jGexJgFzOmMVnl/dAeuV/HBGSgP7Ta3z5YFA==", "dependencies": { "@athenna/ioc": "1.0.9", "@secjs/utils": "1.8.0", @@ -13675,9 +13675,9 @@ } }, "@athenna/logger": { - "version": "1.0.9", - "resolved": "https://registry.npmjs.org/@athenna/logger/-/logger-1.0.9.tgz", - "integrity": "sha512-qJa0VSeKWjLrZ9i0roa3MC2abem8whYA0i2XPHbUZ0WP7mvwCRT2EJZZex1Jv4nB+wLZy4M6f5bFmwOwztksXQ==", + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@athenna/logger/-/logger-1.1.1.tgz", + "integrity": "sha512-cHv846NbsYVPeIhEXUsiGch48sfVLtMNXbK2eFVl7JV32TYP56jGexJgFzOmMVnl/dAeuV/HBGSgP7Ta3z5YFA==", "requires": { "@athenna/ioc": "1.0.9", "@secjs/utils": "1.8.0", diff --git a/package.json b/package.json index 7a86367..263225d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/core", - "version": "1.1.4", + "version": "1.1.5", "description": "", "license": "MIT", "author": "João Lenon ", @@ -164,7 +164,7 @@ "dependencies": { "@athenna/config": "1.0.5", "@athenna/ioc": "1.0.9", - "@athenna/logger": "1.0.9", + "@athenna/logger": "1.1.1", "@athenna/http": "1.1.3", "@secjs/utils": "1.8.0", "reflect-metadata": "0.1.13", diff --git a/src/Application.ts b/src/Application.ts index 2e26832..f64d7e4 100644 --- a/src/Application.ts +++ b/src/Application.ts @@ -135,7 +135,7 @@ export class Application { await this.httpServer.listen(port, host) - this.logger.log(`Http server started on http://${host}:${port}`) + this.logger.success(`Http server started on http://${host}:${port}`) return this.httpServer } @@ -167,7 +167,7 @@ export class Application { private preloadFile(filePath: string) { const { dir, name } = parse(filePath) - this.logger.log(`Preloading ${name} file`) + this.logger.success(`Preloading ${name} file`) require(`${dir}/${name}${this.extension}`) } @@ -184,7 +184,13 @@ export class Application { require(`${dir}/${name}${this.extension}`), ) + this.logger.success('Booting the Http Kernel') + const httpKernel = new HttpKernel() + + /** + * Using getters because they are possible null + */ const container = this.getContainer() const httpServer = this.getHttpServer() @@ -210,19 +216,25 @@ export class Application { /** * Resolving global middlewares inside the Http server. */ - httpKernel.globalMiddlewares.forEach(globalMiddleware => { - globalMiddleware = ResolveClassExport.resolve(globalMiddleware) + httpKernel.globalMiddlewares.forEach(Middleware => { + Middleware = ResolveClassExport.resolve(Middleware) + + if (!container.hasDependency(`App/Middlewares/${Middleware.name}`)) { + container.bind(`App/Middlewares/${Middleware.name}`, Middleware) + } + + Middleware = container.safeUse(`App/Middlewares/${Middleware.name}`) - if (globalMiddleware.handle) { - httpServer.use(globalMiddleware.handle, 'handle') + if (Middleware.handle) { + httpServer.use(Middleware.handle, 'handle') } - if (globalMiddleware.intercept) { - httpServer.use(globalMiddleware.intercept, 'intercept') + if (Middleware.intercept) { + httpServer.use(Middleware.intercept, 'intercept') } - if (globalMiddleware.terminate) { - httpServer.use(globalMiddleware.terminate, 'terminate') + if (Middleware.terminate) { + httpServer.use(Middleware.terminate, 'terminate') } }) } diff --git a/src/Ignite.ts b/src/Ignite.ts index bca6841..72d880e 100644 --- a/src/Ignite.ts +++ b/src/Ignite.ts @@ -197,7 +197,9 @@ export class Ignite { providersNormalized.push(ResolveClassExport.resolve(Provider)) }) - providersNormalized.forEach(p => this.logger.log(`Registering ${p.name}`)) + providersNormalized.forEach(p => + this.logger.success(`Registering ${p.name}`), + ) return providersNormalized } @@ -247,7 +249,7 @@ export class Ignite { preload = normalize(preload) const { dir, name } = parse(Path.config(preload)) - this.logger.log(`Preloading ${name} file`) + this.logger.success(`Preloading ${name} file`) require(`${dir}/${name}${this.extension}`) }) diff --git a/tests/Stubs/config/logging.ts b/tests/Stubs/config/logging.ts index ab4b802..aa5496e 100644 --- a/tests/Stubs/config/logging.ts +++ b/tests/Stubs/config/logging.ts @@ -1,4 +1,5 @@ import { Path } from '@secjs/utils' +import { Color } from '@athenna/logger' export default { /* @@ -22,31 +23,40 @@ export default { | Here you may configure the log channels for your application. | | Available Drivers: "console", "debug", "file". - | Available Formatters: "context", "debug", "json", "log". + | Available Formatters: "cli", "simple", "nest", "json". | */ channels: { application: { driver: 'console', - level: 'INFO', - context: 'Logger', - formatter: 'context', + formatter: 'nest', streamType: 'stdout', + formatterConfig: { + level: 'INFO', + chalk: Color.cyan, + context: 'Logger', + }, }, debug: { driver: 'debug', - level: 'DEBUG', - context: 'Debugger', - formatter: 'context', + formatter: 'nest', namespace: 'api:main', + formatterConfig: { + level: 'DEBUG', + chalk: Color.purple, + context: 'Debugger', + }, }, file: { driver: 'file', - level: 'INFO', - context: 'Logger', - formatter: 'log', + formatter: 'simple', filePath: Path.noBuild().logs('athenna.log'), + formatterConfig: { + level: 'INFO', + chalk: Color.cyan, + context: 'Logger', + }, }, }, }