diff --git a/apps/agw/src/api-gateway/api-gateway.module.ts b/apps/agw/src/api-gateway/api-gateway.module.ts index 0311f3e..c224e12 100644 --- a/apps/agw/src/api-gateway/api-gateway.module.ts +++ b/apps/agw/src/api-gateway/api-gateway.module.ts @@ -1,6 +1,7 @@ -import { Module } from '@nestjs/common'; +import {MiddlewareConsumer, Module} from '@nestjs/common'; import { GraphQLModule } from '@nestjs/graphql'; import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo'; +import {KafkaModule} from "nestjs-kafka"; @Module({ imports: [ @@ -12,4 +13,8 @@ import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo'; }), ], }) -export class ApiGatewayModule {} +export class ApiGatewayModule { + configure(consumer: MiddlewareConsumer) { + consumer.apply(KafkaModule).forRoutes('graphql'); + } +} diff --git a/apps/agw/src/app/app.module.ts b/apps/agw/src/app/app.module.ts index baf8525..212026c 100644 --- a/apps/agw/src/app/app.module.ts +++ b/apps/agw/src/app/app.module.ts @@ -3,9 +3,10 @@ import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import {ApiGatewayModule} from "../api-gateway/api-gateway.module"; +import {KafkaConfigModule} from "../kafka/kafka.module"; @Module({ - imports: [ApiGatewayModule], + imports: [ApiGatewayModule, KafkaConfigModule], controllers: [AppController], providers: [AppService], }) diff --git a/apps/agw/src/kafka/kafka-loger.middleware.ts b/apps/agw/src/kafka/kafka-loger.middleware.ts new file mode 100644 index 0000000..3c80020 --- /dev/null +++ b/apps/agw/src/kafka/kafka-loger.middleware.ts @@ -0,0 +1,14 @@ +import { Injectable, NestMiddleware } from '@nestjs/common'; +import { Request, Response, NextFunction } from 'express'; +import { KafkaProducerService } from './kafka-producer.service'; + +@Injectable() +export class KafkaLoggerMiddleware implements NestMiddleware { + constructor(private readonly kafkaProducer: KafkaProducerService) {} + + async use(req: Request, res: Response, next: NextFunction) { + const logMessage = `Request: ${req.method} ${req.url}`; + await this.kafkaProducer.sendLogMessage('request-logs', logMessage); + next(); + } +} diff --git a/apps/agw/src/kafka/kafka-producer.service.ts b/apps/agw/src/kafka/kafka-producer.service.ts new file mode 100644 index 0000000..f22c772 --- /dev/null +++ b/apps/agw/src/kafka/kafka-producer.service.ts @@ -0,0 +1,18 @@ +import { Injectable } from '@nestjs/common'; +import {KafkaPayload, KafkaService} from 'nestjs-kafka'; + +@Injectable() +export class KafkaProducerService { + TASK_PUSH_INFO = "agw"; + constructor(private readonly kafkaService: KafkaService) {} + + async sendLogMessage(topic: string, message: string): Promise { + const payload: KafkaPayload = { + messageId: '' + new Date().valueOf(), + body: message, + messageType: this.TASK_PUSH_INFO, + topicName: this.TASK_PUSH_INFO, + }; + this.kafkaService.sendMessage('log-kafka', payload); + } +} diff --git a/apps/agw/src/kafka/kafka.module.ts b/apps/agw/src/kafka/kafka.module.ts new file mode 100644 index 0000000..917bddf --- /dev/null +++ b/apps/agw/src/kafka/kafka.module.ts @@ -0,0 +1,16 @@ +import { Module } from '@nestjs/common'; +import { KafkaModule } from 'nestjs-kafka'; +import {KafkaProducerService} from "./kafka-producer.service"; + +@Module({ + imports: [ + KafkaModule.register({ + groupId: "", + clientId: 'nestjs-app', + brokers: ['localhost:9092'] + }), + ], + exports: [KafkaModule], + providers: [KafkaProducerService], +}) +export class KafkaConfigModule {} diff --git a/apps/ods/src/kafka/kafka.module.ts b/apps/ods/src/kafka/kafka.module.ts new file mode 100644 index 0000000..2b2cd77 --- /dev/null +++ b/apps/ods/src/kafka/kafka.module.ts @@ -0,0 +1,4 @@ +import { Module } from '@nestjs/common'; + +@Module({}) +export class KafkaModule {}