-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kafka): implements parser for kafka
There is currently a bug with the integration test for Kafka due to a heartbeat still firing after disconnection. There is an [open issue](nestjs/nest#4830) in Nest's repo about this at the moment. fix #17
- Loading branch information
Showing
21 changed files
with
299 additions
and
3 deletions.
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
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
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,10 @@ | ||
import { ArgumentsHost, Catch, HttpException } from '@nestjs/common'; | ||
import { BaseExceptionFilter } from '@nestjs/core'; | ||
|
||
@Catch() | ||
export class ExceptionFilter extends BaseExceptionFilter { | ||
catch(exception: HttpException, host: ArgumentsHost) { | ||
const res = host.switchToHttp().getResponse(); | ||
res.send(exception); | ||
} | ||
} |
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,41 @@ | ||
import { | ||
Controller, | ||
Get, | ||
Inject, | ||
OnModuleDestroy, | ||
OnModuleInit, | ||
UseFilters, | ||
} from '@nestjs/common'; | ||
import { ClientKafka } from '@nestjs/microservices'; | ||
import { ExceptionFilter } from './exception.filter'; | ||
|
||
@Controller() | ||
export class KafkaClientController implements OnModuleInit, OnModuleDestroy { | ||
constructor(@Inject('KAFKA_SERVICE') private readonly kafka: ClientKafka) {} | ||
|
||
async onModuleInit() { | ||
['hello', 'error', 'skip'].forEach((key) => | ||
this.kafka.subscribeToResponseOf(`say.${key}`), | ||
); | ||
} | ||
|
||
onModuleDestroy() { | ||
this.kafka.close(); | ||
} | ||
|
||
@Get() | ||
sayHello() { | ||
return this.kafka.send('say.hello', { ip: '127.0.0.1' }); | ||
} | ||
|
||
@Get('error') | ||
@UseFilters(ExceptionFilter) | ||
sayError() { | ||
return this.kafka.send('say.error', { ip: '127.0.0.1' }); | ||
} | ||
|
||
@Get('skip') | ||
saySkip() { | ||
return this.kafka.send('say.skip', { ip: '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,25 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { ClientsModule, Transport } from '@nestjs/microservices'; | ||
import { KafkaClientController } from './kafka-client.controller'; | ||
|
||
@Module({ | ||
imports: [ | ||
ClientsModule.register([ | ||
{ | ||
name: 'KAFKA_SERVICE', | ||
transport: Transport.KAFKA, | ||
options: { | ||
client: { | ||
clientId: 'client', | ||
brokers: ['localhost:9092'], | ||
}, | ||
consumer: { | ||
groupId: 'client-consumer', | ||
}, | ||
}, | ||
}, | ||
]), | ||
], | ||
controllers: [KafkaClientController], | ||
}) | ||
export class KafkaClientModule {} |
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,10 @@ | ||
import { Catch, HttpException } from '@nestjs/common'; | ||
import { BaseRpcExceptionFilter } from '@nestjs/microservices'; | ||
import { Observable, throwError } from 'rxjs'; | ||
|
||
@Catch() | ||
export class ExceptionFilter extends BaseRpcExceptionFilter { | ||
catch(exception: HttpException): Observable<any> { | ||
return throwError(exception.message); | ||
} | ||
} |
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,27 @@ | ||
import { BadRequestException, Controller, UseFilters } from '@nestjs/common'; | ||
import { MessagePattern } from '@nestjs/microservices'; | ||
import { OgmaSkip } from '@ogma/nestjs-module'; | ||
import { AppService } from '../../app.service'; | ||
import { ExceptionFilter } from './exception.filter'; | ||
|
||
@Controller() | ||
export class KafkaServerController { | ||
constructor(private readonly service: AppService) {} | ||
|
||
@MessagePattern('say.hello') | ||
sayHello() { | ||
return this.service.getHello(); | ||
} | ||
|
||
@UseFilters(ExceptionFilter) | ||
@MessagePattern('say.error') | ||
sayError() { | ||
throw new BadRequestException('Borked'); | ||
} | ||
|
||
@OgmaSkip() | ||
@MessagePattern('say.skip') | ||
saySkip() { | ||
return this.service.getHello(); | ||
} | ||
} |
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,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AppService } from '../../app.service'; | ||
import { KafkaServerController } from './kafka-server.controller'; | ||
|
||
@Module({ | ||
controllers: [KafkaServerController], | ||
providers: [AppService], | ||
}) | ||
export class KafkaServerModule {} |
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 @@ | ||
describe('gRPC test', () => { | ||
it.todo('implement test'); | ||
}); |
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,93 @@ | ||
import { INestApplication, INestMicroservice } from '@nestjs/common'; | ||
import { MicroserviceOptions, Transport } from '@nestjs/microservices'; | ||
import { Test } from '@nestjs/testing'; | ||
import { color } from '@ogma/logger'; | ||
import { OgmaInterceptor, OgmaModule } from '@ogma/nestjs-module'; | ||
import { KafkaParser } from '@ogma/platform-kafka'; | ||
import { KafkaClientModule } from '../src/kafka/client/kafka-client.module'; | ||
import { KafkaServerModule } from '../src/kafka/server/kafka-server.module'; | ||
import { getInterceptor, hello, httpPromise } from './utils'; | ||
|
||
describe('kafka test', () => { | ||
let interceptor: OgmaInterceptor; | ||
let server: INestMicroservice; | ||
let client: INestApplication; | ||
|
||
beforeAll(async () => { | ||
const serverModRef = await Test.createTestingModule({ | ||
imports: [ | ||
KafkaServerModule, | ||
OgmaModule.forRoot({ | ||
interceptor: { | ||
rpc: KafkaParser, | ||
}, | ||
}), | ||
], | ||
}).compile(); | ||
server = serverModRef.createNestMicroservice<MicroserviceOptions>({ | ||
transport: Transport.KAFKA, | ||
options: { | ||
client: { | ||
brokers: ['localhost:9092'], | ||
}, | ||
}, | ||
}); | ||
interceptor = getInterceptor(server); | ||
await server.listenAsync(); | ||
const clientModRef = await Test.createTestingModule({ | ||
imports: [KafkaClientModule], | ||
}).compile(); | ||
client = clientModRef.createNestApplication(); | ||
await client.listen(0); | ||
}); | ||
|
||
afterAll(async () => { | ||
await client.close(); | ||
await server.close(); | ||
}); | ||
|
||
describe('server calls', () => { | ||
let logSpy: jest.SpyInstance; | ||
let baseUrl: string; | ||
|
||
beforeAll(async () => { | ||
baseUrl = await client.getUrl(); | ||
}); | ||
|
||
beforeEach(() => { | ||
logSpy = jest.spyOn(interceptor, 'log'); | ||
}); | ||
|
||
afterEach(() => { | ||
logSpy.mockClear(); | ||
}); | ||
|
||
it.each` | ||
url | status | endpoint | ||
${'/'} | ${color.green(200)} | ${'say.hello'} | ||
${'/error'} | ${color.red(500)} | ${'say.error'} | ||
`( | ||
'$url call', | ||
async ({ | ||
url, | ||
status, | ||
endpoint, | ||
}: { | ||
url: string; | ||
status: string; | ||
endpoint: string; | ||
}) => { | ||
await httpPromise(baseUrl + url); | ||
expect(logSpy).toBeCalledTimes(1); | ||
const logObject = logSpy.mock.calls[0][0]; | ||
expect(logObject).toBeALogObject('Kafka', endpoint, 'kafka', status); | ||
}, | ||
); | ||
|
||
it('should call the skip but not log it', async () => { | ||
const data = await httpPromise(baseUrl + '/skip'); | ||
expect(logSpy).toHaveBeenCalledTimes(0); | ||
expect(data).toEqual(hello); | ||
}); | ||
}); | ||
}); |
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
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
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 * from './kafka-interceptor.service'; |
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,40 @@ | ||
import { ExecutionContext, Injectable } from '@nestjs/common'; | ||
import { KafkaContext } from '@nestjs/microservices'; | ||
import { AbstractInterceptorService } from '@ogma/nestjs-module'; | ||
|
||
@Injectable() | ||
export class KafkaParser extends AbstractInterceptorService { | ||
getCallPoint(context: ExecutionContext) { | ||
return this.getClient(context).getTopic(); | ||
} | ||
|
||
getCallerIp(context: ExecutionContext) { | ||
const data = this.getData(context); | ||
return data?.value?.ip || ''; | ||
} | ||
|
||
getMethod() { | ||
return 'Kafka'; | ||
} | ||
|
||
getProtocol() { | ||
return 'kafka'; | ||
} | ||
|
||
getStatus( | ||
context: ExecutionContext, | ||
inColor: boolean, | ||
error?: Error | ExecutionContext, | ||
): string { | ||
const status = error ? 500 : 200; | ||
return inColor ? this.wrapInColor(status) : status.toString(); | ||
} | ||
|
||
private getClient(context: ExecutionContext): KafkaContext { | ||
return context.switchToRpc().getContext<KafkaContext>(); | ||
} | ||
|
||
private getData(context: ExecutionContext): any { | ||
return context.switchToRpc().getData(); | ||
} | ||
} |
Empty file.
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