parent
1a05a7b455
commit
331d0a0729
@ -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();
|
||||
}
|
||||
}
|
@ -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<void> {
|
||||
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);
|
||||
}
|
||||
}
|
@ -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 {}
|
@ -0,0 +1,4 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
|
||||
@Module({})
|
||||
export class KafkaModule {}
|
Loading…
Reference in new issue