HM-38. Добавлен логгер в бек, ведет записи в базу, добавлена ручка для получения логов
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -3,7 +3,6 @@
|
||||
/node_modules
|
||||
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
|
||||
@ -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 {}
|
||||
@ -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'];
|
||||
|
||||
25
src/logs/logs.controller.ts
Normal file
25
src/logs/logs.controller.ts
Normal file
@ -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<Log[]> {
|
||||
const logsList = await this.logsService.findAll();
|
||||
return logsList;
|
||||
}
|
||||
}
|
||||
19
src/logs/logs.schema.ts
Normal file
19
src/logs/logs.schema.ts
Normal file
@ -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);
|
||||
20
src/logs/logs.service.ts
Normal file
20
src/logs/logs.service.ts
Normal file
@ -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<Log> {
|
||||
return this.dbConnection.model<Log>(COLLECTION_LOGS, LogSchema);
|
||||
}
|
||||
|
||||
async findAll(): Promise<Log[]> {
|
||||
return this.logModel.find().exec();
|
||||
}
|
||||
}
|
||||
@ -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')
|
||||
|
||||
37
src/services/logger.service.ts
Normal file
37
src/services/logger.service.ts
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
|
||||
@ -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<Store> {
|
||||
if (api === DB_TEST_NAME) {
|
||||
return this.dbTestConnection.model<Store>(COOLECTION_STORE, StoreSchema);
|
||||
return this.dbTestConnection.model<Store>(COLLECTION_STORE, StoreSchema);
|
||||
}
|
||||
return this.dbConnection.model<Store>(COOLECTION_STORE, StoreSchema);
|
||||
return this.dbConnection.model<Store>(COLLECTION_STORE, StoreSchema);
|
||||
}
|
||||
|
||||
async findAll(api: string): Promise<Store[]> {
|
||||
|
||||
@ -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
|
||||
DELETE http://localhost:4001/store/testApi-433 HTTP/1.1
|
||||
Api-Name: store-service-test
|
||||
|
||||
###
|
||||
|
||||
GET http://localhost:4001/logs HTTP/1.1
|
||||
|
||||
Reference in New Issue
Block a user