Skip to content

Commit

Permalink
Организует логи, опишет схему и конфиг, добавит внедрение зависимостей.
Browse files Browse the repository at this point in the history
  • Loading branch information
RnizereB committed Oct 20, 2024
1 parent 4e0baac commit 13b93d3
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
.DS_Store
coverage
*.log
.env
19 changes: 19 additions & 0 deletions src/main.rest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'reflect-metadata';
import { RestApplication } from './rest/index.js';
import { Config, RestConfig, RestSchema } from './shared/libs/config/index.js';
import { Logger, PinoLogger} from './shared/libs/logger/index.js';
import { Container } from 'inversify';
import { Component } from './shared/types/index.js';

async function bootstrap() {
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 = container.get<RestApplication>(Component.RestApplication);
await application.init();
}

bootstrap();
1 change: 1 addition & 0 deletions src/rest/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { RestApplication } from './rest.application.js';
18 changes: 18 additions & 0 deletions src/rest/rest.application.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { inject, injectable } from 'inversify';
import { Config, RestSchema } from '../shared/libs/config/index.js';
import { Logger } from '../shared/libs/logger/index.js';
import { Component } from '../shared/types/index.js';


@injectable()
export class RestApplication {
constructor(
@inject(Component.Logger) private readonly logger: Logger,
@inject(Component.Config) private readonly config: Config<RestSchema>
) {}

public async init() {
this.logger.info('Application initiazation');
this.logger.info(`Get value from env $PORT: ${this.config.get('PORT')}`);
}
}
2 changes: 1 addition & 1 deletion src/shared/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {getRandomElement, getRandomNumber, getRandomItems} from './functions.js';
export { getRandomElement, getRandomNumber, getRandomItems } from './functions.js';
3 changes: 3 additions & 0 deletions src/shared/libs/config/config.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface Config<U> {
get<T extends keyof U>(key: T): U[T];
}
3 changes: 3 additions & 0 deletions src/shared/libs/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { RestConfig } from './rest.config.js';
export { Config } from './config.interface.js';
export { RestSchema, configRestSchema } from './rest.schema.js';
30 changes: 30 additions & 0 deletions src/shared/libs/config/rest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { config } from 'dotenv';
import { Logger } from '../logger/index.js';
import { Config, RestSchema, configRestSchema } from './index.js';
import { inject, injectable } from 'inversify';
import { Component } from '../../types/component.enum.js';


@injectable()
export class RestConfig implements Config<RestSchema> {
private readonly config: RestSchema;

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

if (parsedOutput.error) {
throw new Error ('Can\'t read .env file. Perhaps the file does not exists.');
}
configRestSchema.load({});
configRestSchema.validate({ allowed: 'strict', output: this.logger.info });

this.config = configRestSchema.getProperties();
this.logger.info('.env file found and successfully parsed!');
}

public get<T extends keyof RestSchema>(key: T): RestSchema[T] {
return this.config[key];
}
}
31 changes: 31 additions & 0 deletions src/shared/libs/config/rest.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import convict from 'convict';
import validator from 'convict-format-with-validator';

convict.addFormats(validator);

export type RestSchema = {
PORT: number,
SALT: string,
HOST_DB: string,
}

export const configRestSchema = convict<RestSchema>({
PORT: {
doc: 'Port for incoming connections',
format: 'port',
env: 'PORT',
default: 4000
},
SALT: {
doc: 'Salt for password hash',
format: String,
env: 'SALT',
default: null
},
HOST_DB: {
doc: 'IP address of the database server (MongoDB)',
format: 'ipaddress',
env: 'HOST_DB',
default: '127.0.0.1'
}
});
2 changes: 2 additions & 0 deletions src/shared/libs/logger/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { Logger} from './logger.interface.js';
export { PinoLogger } from './pino.logger.js';
6 changes: 6 additions & 0 deletions src/shared/libs/logger/logger.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface Logger {
info(message: string, ...args: unknown[]): void;
warn(message: string, ...args: unknown[]): void;
error(message: string, error: Error, ...args: unknown[]): void;
debug(message: string, ...args: unknown[]): void;
}
28 changes: 28 additions & 0 deletions src/shared/libs/logger/pino.logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Logger as PinoInstance, pino} from 'pino';
import { Logger } from './index.js';
import { injectable } from 'inversify';

@injectable()
export class PinoLogger implements Logger {
private readonly logger: PinoInstance;

constructor() {
this.logger = pino();
}

public info(message: string, ...args: unknown[]): void {
this.logger.info(message, ...args);
}

public warn(message: string, ...args: unknown[]): void {
this.logger.warn(message, ...args);
}

public error(message: string, error: Error, ...args: unknown[]): void {
this.logger.error(error, message, ...args);
}

public debug(message: string, ...args: unknown[]): void {
this.logger.debug(message, ...args);
}
}
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
@@ -1,3 +1,4 @@
export { Offer } from './offer-type.js';
export { TypeUser, User } from './user-type.js';
export { MockServerData } from './mock-server-data.js';
export { Component } from './component.enum.js';

0 comments on commit 13b93d3

Please sign in to comment.