Skip to content

Commit

Permalink
Делегирует создание зависимостей с помощью инверсии управления библио…
Browse files Browse the repository at this point in the history
…теки inversify
  • Loading branch information
AdonaiJehosua committed Aug 24, 2024
1 parent 6449897 commit eeedc80
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 10 deletions.
26 changes: 25 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
15 changes: 10 additions & 5 deletions src/main.rest.ts
Original file line number Diff line number Diff line change
@@ -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<RestApplication>(Component.RestApplication).to(RestApplication).inSingletonScope();
container.bind<Logger>(Component.Logger).to(PinoLogger).inSingletonScope();
container.bind<Config<RestSchema>>(Component.Config).to(RestConfig).inSingletonScope();

const application = new RestApplication(logger, config);
const application = container.get<RestApplication>(Component.RestApplication);
await application.init();
}

Expand Down
7 changes: 5 additions & 2 deletions src/rest/rest.application.ts
Original file line number Diff line number Diff line change
@@ -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<RestSchema>,
@inject(Component.Logger) private readonly logger: Logger,
@inject(Component.Config) private readonly config: Config<RestSchema>,
) {}

public async init() {
Expand Down
5 changes: 4 additions & 1 deletion src/shared/libs/config/rest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<RestSchema> {
private readonly config: RestSchema;

constructor(
private readonly logger: Logger
@inject(Component.Logger) private readonly logger: Logger
) {
const parsedOutput = config();

Expand Down
2 changes: 2 additions & 0 deletions src/shared/libs/logger/pino.logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
5 changes: 5 additions & 0 deletions src/shared/types/component.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const Component = {
RestApplication: Symbol.for('RestApplication'),
Logger: Symbol.for('Logger'),
Config: Symbol.for('Config'),
} as const;
1 change: 1 addition & 0 deletions src/shared/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

0 comments on commit eeedc80

Please sign in to comment.