-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Организует логи, опишет схему и конфиг, добавит внедрение зависимостей.
- Loading branch information
Showing
14 changed files
with
149 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ | |
.DS_Store | ||
coverage | ||
*.log | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { RestApplication } from './rest.application.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |