From eeedc80a7543cfe7cdee29e0ee84829a02aab0c2 Mon Sep 17 00:00:00 2001 From: AdonaiJehosua Date: Sat, 24 Aug 2024 15:33:03 +0500 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=B5=D0=BB=D0=B5=D0=B3=D0=B8=D1=80?= =?UTF-8?q?=D1=83=D0=B5=D1=82=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=B7=D0=B0=D0=B2=D0=B8=D1=81=D0=B8=D0=BC=D0=BE=D1=81?= =?UTF-8?q?=D1=82=D0=B5=D0=B9=20=D1=81=20=D0=BF=D0=BE=D0=BC=D0=BE=D1=89?= =?UTF-8?q?=D1=8C=D1=8E=20=D0=B8=D0=BD=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D0=B8?= =?UTF-8?q?=20=D1=83=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=B1=D0=B8=D0=B1=D0=BB=D0=B8=D0=BE=D1=82=D0=B5=D0=BA=D0=B8?= =?UTF-8?q?=20inversify?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 26 +++++++++++++++++++++++++- package.json | 4 +++- src/main.rest.ts | 15 ++++++++++----- src/rest/rest.application.ts | 7 +++++-- src/shared/libs/config/rest.config.ts | 5 ++++- src/shared/libs/logger/pino.logger.ts | 2 ++ src/shared/types/component.enum.ts | 5 +++++ src/shared/types/index.ts | 1 + 8 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 src/shared/types/component.enum.ts diff --git a/package-lock.json b/package-lock.json index e86726e..2e30a44 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,9 @@ "convict-format-with-validator": "6.2.0", "dotenv": "16.3.1", "got": "13.0.0", - "pino": "8.15.1" + "inversify": "6.0.1", + "pino": "8.15.1", + "reflect-metadata": "0.1.13" }, "devDependencies": { "@types/convict": "6.1.4", @@ -3997,6 +3999,12 @@ "node": ">= 0.4" } }, + "node_modules/inversify": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/inversify/-/inversify-6.0.1.tgz", + "integrity": "sha512-B3ex30927698TJENHR++8FfEaJGqoWOgI6ZY5Ht/nLUsFCwHn6akbwtnUAPCgUepAnTpe2qHxhDNjoKLyz6rgQ==", + "license": "MIT" + }, "node_modules/ipaddr.js": { "version": "1.9.1", "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", @@ -5794,6 +5802,12 @@ "node": ">= 12.13.0" } }, + "node_modules/reflect-metadata": { + "version": "0.1.13", + "resolved": "https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.1.13.tgz", + "integrity": "sha512-Ts1Y/anZELhSsjMcU605fU9RE4Oi3p5ORujwbIKXfWa+0Zxs510Qrmrce5/Jowq3cHSZSJqBjypxmHarc+vEWg==", + "license": "Apache-2.0" + }, "node_modules/regexp-tree": { "version": "0.1.27", "resolved": "https://registry.npmjs.org/regexp-tree/-/regexp-tree-0.1.27.tgz", @@ -9898,6 +9912,11 @@ "side-channel": "^1.0.4" } }, + "inversify": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/inversify/-/inversify-6.0.1.tgz", + "integrity": "sha512-B3ex30927698TJENHR++8FfEaJGqoWOgI6ZY5Ht/nLUsFCwHn6akbwtnUAPCgUepAnTpe2qHxhDNjoKLyz6rgQ==" + }, "ipaddr.js": { "version": "1.9.1", "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", @@ -11152,6 +11171,11 @@ "resolved": "https://registry.npmjs.org/real-require/-/real-require-0.2.0.tgz", "integrity": "sha512-57frrGM/OCTLqLOAh0mhVA9VBMHd+9U7Zb2THMGdBUoZVOtGbJzjxsYGDJ3A9AYYCP4hn6y1TVbaOfzWtm5GFg==" }, + "reflect-metadata": { + "version": "0.1.13", + "resolved": "https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.1.13.tgz", + "integrity": "sha512-Ts1Y/anZELhSsjMcU605fU9RE4Oi3p5ORujwbIKXfWa+0Zxs510Qrmrce5/Jowq3cHSZSJqBjypxmHarc+vEWg==" + }, "regexp-tree": { "version": "0.1.27", "resolved": "https://registry.npmjs.org/regexp-tree/-/regexp-tree-0.1.27.tgz", diff --git a/package.json b/package.json index 134df97..902a79e 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,8 @@ "convict-format-with-validator": "6.2.0", "dotenv": "16.3.1", "got": "13.0.0", - "pino": "8.15.1" + "inversify": "6.0.1", + "pino": "8.15.1", + "reflect-metadata": "0.1.13" } } diff --git a/src/main.rest.ts b/src/main.rest.ts index 8a9c3a6..dea2ba2 100644 --- a/src/main.rest.ts +++ b/src/main.rest.ts @@ -1,12 +1,17 @@ -import { PinoLogger } from './shared/libs/logger/index.js'; +import 'reflect-metadata'; +import {Logger, PinoLogger} from './shared/libs/logger/index.js'; import { RestApplication } from './rest/index.js'; -import {RestConfig} from './shared/libs/config/index.js'; +import {Config, RestConfig, RestSchema} from './shared/libs/config/index.js'; +import {Container} from 'inversify'; +import {Component} from './shared/types/index.js'; async function bootstrap() { - const logger = new PinoLogger(); - const config = new RestConfig(logger); + const container = new Container(); + container.bind(Component.RestApplication).to(RestApplication).inSingletonScope(); + container.bind(Component.Logger).to(PinoLogger).inSingletonScope(); + container.bind>(Component.Config).to(RestConfig).inSingletonScope(); - const application = new RestApplication(logger, config); + const application = container.get(Component.RestApplication); await application.init(); } diff --git a/src/rest/rest.application.ts b/src/rest/rest.application.ts index e4b088f..4a94360 100644 --- a/src/rest/rest.application.ts +++ b/src/rest/rest.application.ts @@ -1,10 +1,13 @@ import { Logger } from '../shared/libs/logger/index.js'; import {Config, RestSchema} from '../shared/libs/config/index.js'; +import {Component} from '../shared/types/index.js'; +import {inject, injectable} from 'inversify'; +@injectable() export class RestApplication { constructor( - private readonly logger: Logger, - private readonly config: Config, + @inject(Component.Logger) private readonly logger: Logger, + @inject(Component.Config) private readonly config: Config, ) {} public async init() { diff --git a/src/shared/libs/config/rest.config.ts b/src/shared/libs/config/rest.config.ts index 77810ab..ba2b725 100644 --- a/src/shared/libs/config/rest.config.ts +++ b/src/shared/libs/config/rest.config.ts @@ -2,12 +2,15 @@ import {Config} from './config.interface.js'; import {Logger} from '../logger/index.js'; import {config} from 'dotenv'; import {configRestSchema, RestSchema} from './rest.schema.js'; +import {Component} from '../../types/index.js'; +import {inject, injectable} from 'inversify'; +@injectable() export class RestConfig implements Config { private readonly config: RestSchema; constructor( - private readonly logger: Logger + @inject(Component.Logger) private readonly logger: Logger ) { const parsedOutput = config(); diff --git a/src/shared/libs/logger/pino.logger.ts b/src/shared/libs/logger/pino.logger.ts index 3c58a8e..1e299a2 100644 --- a/src/shared/libs/logger/pino.logger.ts +++ b/src/shared/libs/logger/pino.logger.ts @@ -2,7 +2,9 @@ import {Logger as PinoInstance, pino, transport} from 'pino'; import {Logger} from './logger.interface.js'; import {getCurrentModuleDirectoryPath} from '../../helpers/index.js'; import {resolve} from 'node:path'; +import { injectable } from 'inversify'; +@injectable() export class PinoLogger implements Logger { private readonly logger: PinoInstance; diff --git a/src/shared/types/component.enum.ts b/src/shared/types/component.enum.ts new file mode 100644 index 0000000..c7a25ed --- /dev/null +++ b/src/shared/types/component.enum.ts @@ -0,0 +1,5 @@ +export const Component = { + RestApplication: Symbol.for('RestApplication'), + Logger: Symbol.for('Logger'), + Config: Symbol.for('Config'), +} as const; diff --git a/src/shared/types/index.ts b/src/shared/types/index.ts index 72d977c..81f5d11 100644 --- a/src/shared/types/index.ts +++ b/src/shared/types/index.ts @@ -6,3 +6,4 @@ export * from './offer-type.enum.js'; export * from './offer-good.enum.js'; export * from './mock-server-data.type.js'; export * from './good.union-type.js'; +export * from './component.enum.js';