diff --git a/.gitignore b/.gitignore index c16ef02..bb2354e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,6 @@ /node_modules # Logs -logs *.log npm-debug.log* yarn-debug.log* diff --git a/src/app.module.ts b/src/app.module.ts index 3d9162a..b9c4d56 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,9 +1,12 @@ import {Module} from '@nestjs/common'; import {MongooseModule} from '@nestjs/mongoose'; -import {MONGO_URL, DB_NAME, DB_TEST_NAME} from './consts'; +import {MONGO_URL, DB_NAME, DB_TEST_NAME, DB_LOGGER} from './consts'; import {StoreService} from './store/store.service'; import {Store, StoreSchema} from './store/store.schema'; import {StoreController} from './store/store.controller'; +import {Log, LogSchema} from './logs/logs.schema'; +import {LogsService} from './logs/logs.service'; +import {LogsController} from './logs/logs.controller'; @Module({ imports: [ @@ -13,18 +16,26 @@ import {StoreController} from './store/store.controller'; MongooseModule.forRoot(`${MONGO_URL}/${DB_TEST_NAME}`, { connectionName: DB_TEST_NAME, }), + MongooseModule.forRoot(`${MONGO_URL}/${DB_LOGGER}`, { + connectionName: DB_LOGGER, + }), MongooseModule.forFeature([ {name: Store.name, schema: StoreSchema}, ], DB_NAME), MongooseModule.forFeature([ {name: Store.name, schema: StoreSchema}, ], DB_TEST_NAME), + MongooseModule.forFeature([ + {name: Log.name, schema: LogSchema}, + ], DB_LOGGER), ], controllers: [ StoreController, + LogsController, ], providers: [ StoreService, + LogsService, ] }) export class AppModule {} \ No newline at end of file diff --git a/src/consts.ts b/src/consts.ts index c6c4e91..df4e3ea 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -1,7 +1,9 @@ export const DB_NAME = 'store-service'; export const DB_TEST_NAME = 'store-service-test'; +export const DB_LOGGER = 'logger'; export const MONGO_URL = 'mongodb://localhost:27017'; -export const COOLECTION_STORE = 'store'; +export const COLLECTION_STORE = 'store'; +export const COLLECTION_LOGS = 'logs'; export const ALLOW_ORIGIN_ALL: [string, string] = ['Access-Control-Allow-Origin', '*']; export const ALLOW_CREDENTIALS: [string, string] = ['Access-Control-Allow-Credentials', 'true']; export const CONTENT_LENGTH: [string, string] = ['Content-Length', '0']; diff --git a/src/logs/logs.controller.ts b/src/logs/logs.controller.ts new file mode 100644 index 0000000..1fb4ebf --- /dev/null +++ b/src/logs/logs.controller.ts @@ -0,0 +1,25 @@ +import {Controller, Get, Header} from '@nestjs/common'; +import {ApiTags, ApiResponse} from '@nestjs/swagger'; +import {LogsService} from './logs.service'; +import {ALLOW_ORIGIN_ALL, COLLECTION_LOGS} from 'src/consts'; +import {Log, LogRequest} from './logs.schema'; + +@Controller(COLLECTION_LOGS) +@ApiTags(COLLECTION_LOGS) +export class LogsController { + constructor( + private readonly logsService: LogsService + ) {} + + @Get() + @Header(...ALLOW_ORIGIN_ALL) + @ApiResponse({ + status: 200, + description: 'Возвращает список всех логов', + type: [LogRequest], + }) + async findAll(): Promise { + const logsList = await this.logsService.findAll(); + return logsList; + } +} diff --git a/src/logs/logs.schema.ts b/src/logs/logs.schema.ts new file mode 100644 index 0000000..a67f4af --- /dev/null +++ b/src/logs/logs.schema.ts @@ -0,0 +1,19 @@ +import { Document } from 'mongoose'; +import {Prop, Schema, SchemaFactory} from '@nestjs/mongoose'; +import {ApiProperty} from '@nestjs/swagger'; + +export class LogRequest { + @ApiProperty() + message: string; +} + +@Schema() +export class Log extends Document { + @Prop({ + required: true, + type: String, + }) + message: string; +} + +export const LogSchema = SchemaFactory.createForClass(Log); diff --git a/src/logs/logs.service.ts b/src/logs/logs.service.ts new file mode 100644 index 0000000..d4ee651 --- /dev/null +++ b/src/logs/logs.service.ts @@ -0,0 +1,20 @@ +import {Injectable} from '@nestjs/common'; +import {InjectConnection} from '@nestjs/mongoose'; +import {DB_LOGGER, COLLECTION_LOGS} from 'src/consts'; +import {Connection, Model} from 'mongoose'; +import {LogSchema, Log} from './logs.schema'; + +@Injectable() +export class LogsService { + constructor( + @InjectConnection(DB_LOGGER) private dbConnection: Connection, + ) {} + + get logModel(): Model { + return this.dbConnection.model(COLLECTION_LOGS, LogSchema); + } + + async findAll(): Promise { + return this.logModel.find().exec(); + } +} diff --git a/src/main.ts b/src/main.ts index 7975d47..305e463 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,9 +1,12 @@ import {NestFactory} from '@nestjs/core'; import {SwaggerModule, DocumentBuilder} from '@nestjs/swagger'; import {AppModule} from './app.module'; +import {Logger} from './services/logger.service'; async function bootstrap() { - const app = await NestFactory.create(AppModule); + const app = await NestFactory.create(AppModule, { + logger: new Logger(), + }); const options = new DocumentBuilder() .setTitle('Store API') diff --git a/src/services/logger.service.ts b/src/services/logger.service.ts new file mode 100644 index 0000000..3a64ab9 --- /dev/null +++ b/src/services/logger.service.ts @@ -0,0 +1,37 @@ +import * as mongoose from 'mongoose'; + +import {Logger as DefaultLogger} from '@nestjs/common'; +import {MONGO_URL} from 'src/consts'; + +mongoose.connect(`${MONGO_URL}/logger`, {useNewUrlParser: true}); + +const errorSchema = new mongoose.Schema({ + message: String, +}); +const ErrorModel = mongoose.model('logs', errorSchema); + +export class Logger extends DefaultLogger { + log(message: string): void { + super.log(message); + } + error(message: string, trace: string): void { + super.error(message, trace); + const error = new ErrorModel({ + message: `ERROR: message = ${message}, trace = ${trace}` + }); + error.save(); + } + warn(message: string): void { + super.warn(message); + const error = new ErrorModel({ + message: `WARN: message = ${message}` + }); + error.save(); + } + debug(message: string): void { + super.debug(message); + } + verbose(message: string): void { + super.verbose(message); + } +} \ No newline at end of file diff --git a/src/store/store.controller.ts b/src/store/store.controller.ts index 84ddb58..066627b 100644 --- a/src/store/store.controller.ts +++ b/src/store/store.controller.ts @@ -2,7 +2,7 @@ import {Controller, Get, Req, Post, Options, Header, Delete, HttpCode, Put} from import {StoreService} from './store.service'; import {Store, StoreRequest} from './store.schema'; import {ApiResponse, ApiTags, ApiParam, ApiBody} from '@nestjs/swagger'; -import {ALLOW_ORIGIN_ALL, ALLOW_METHOD, ALLOW_CREDENTIALS, CONTENT_LENGTH, ALLOW_HEADERS, COOLECTION_STORE} from 'src/consts'; +import {ALLOW_ORIGIN_ALL, ALLOW_METHOD, ALLOW_CREDENTIALS, CONTENT_LENGTH, ALLOW_HEADERS, COLLECTION_STORE} from 'src/consts'; import {Request} from 'express'; const prepareStoreToStoreRequest = ({ @@ -16,8 +16,8 @@ const makeApiHeader = (request: Request): string => { return typeof apiHeader === 'string' ? apiHeader : ''; }; -@Controller(COOLECTION_STORE) -@ApiTags(COOLECTION_STORE) +@Controller(COLLECTION_STORE) +@ApiTags(COLLECTION_STORE) export class StoreController { constructor( private readonly storeService: StoreService diff --git a/src/store/store.service.ts b/src/store/store.service.ts index 3411ef6..2568790 100644 --- a/src/store/store.service.ts +++ b/src/store/store.service.ts @@ -2,7 +2,7 @@ import {Model, Connection} from 'mongoose'; import {Injectable, NotFoundException, BadGatewayException} from '@nestjs/common'; import {InjectConnection} from '@nestjs/mongoose'; import {Store, StoreRequest, StoreSchema} from './store.schema'; -import {DB_TEST_NAME, DB_NAME, COOLECTION_STORE} from 'src/consts'; +import {DB_TEST_NAME, DB_NAME, COLLECTION_STORE} from 'src/consts'; const validateModel = async (store: Store) => { try { @@ -21,9 +21,9 @@ export class StoreService { storeModel(api: string): Model { if (api === DB_TEST_NAME) { - return this.dbTestConnection.model(COOLECTION_STORE, StoreSchema); + return this.dbTestConnection.model(COLLECTION_STORE, StoreSchema); } - return this.dbConnection.model(COOLECTION_STORE, StoreSchema); + return this.dbConnection.model(COLLECTION_STORE, StoreSchema); } async findAll(api: string): Promise { diff --git a/store.http b/store.http index 9f8a63d..311f313 100644 --- a/store.http +++ b/store.http @@ -13,7 +13,7 @@ content-type: application/json Api-Name: store-service-test { - "key": "testApi-4", + "key": "testApi-455", "value": { }, @@ -40,5 +40,9 @@ Api-Name: store-service-test ### -DELETE http://localhost:4001/store/testApi-4 HTTP/1.1 -Api-Name: store-service-test \ No newline at end of file +DELETE http://localhost:4001/store/testApi-433 HTTP/1.1 +Api-Name: store-service-test + +### + +GET http://localhost:4001/logs HTTP/1.1