forked from nestjs/nest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(@nestjs/microservices) add grpc, mqtt, microservices refactor
- Loading branch information
1 parent
a18e4c6
commit 852ce08
Showing
121 changed files
with
5,079 additions
and
1,234 deletions.
There are no files selected for viewing
Binary file not shown.
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
Binary file not shown.
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,38 @@ | ||
import * as express from 'express'; | ||
import * as request from 'supertest'; | ||
import { Test } from '@nestjs/testing'; | ||
import { INestApplication } from '@nestjs/common'; | ||
import { Transport } from '@nestjs/microservices'; | ||
import { MqttMulticastController } from '../src/mqtt/mqtt-multicast.controller'; | ||
|
||
describe('MQTT transport', () => { | ||
let server; | ||
let app: INestApplication; | ||
|
||
beforeEach(async () => { | ||
const module = await Test.createTestingModule({ | ||
controllers: [MqttMulticastController], | ||
}).compile(); | ||
|
||
server = express(); | ||
app = module.createNestApplication(server); | ||
app.connectMicroservice({ | ||
transport: Transport.MQTT, | ||
}); | ||
app.connectMicroservice({ | ||
transport: Transport.MQTT, | ||
}); | ||
await app.startAllMicroservicesAsync(); | ||
await app.init(); | ||
}); | ||
|
||
it(`Multicast (2 subscribers)`, () => { | ||
return request(server) | ||
.get('/multicast') | ||
.expect(200, '2'); | ||
}); | ||
|
||
afterEach(async () => { | ||
await app.close(); | ||
}); | ||
}); |
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 * as express from 'express'; | ||
import * as request from 'supertest'; | ||
import { Test } from '@nestjs/testing'; | ||
import { INestApplication } from '@nestjs/common'; | ||
import { Transport } from '@nestjs/microservices'; | ||
import { join } from 'path'; | ||
import { GrpcController } from '../src/grpc/grpc.controller'; | ||
|
||
describe('GRPC transport', () => { | ||
let server; | ||
let app: INestApplication; | ||
|
||
beforeEach(async () => { | ||
const module = await Test.createTestingModule({ | ||
controllers: [GrpcController], | ||
}).compile(); | ||
|
||
server = express(); | ||
app = module.createNestApplication(server); | ||
app.connectMicroservice({ | ||
transport: Transport.GRPC, | ||
options: { | ||
package: 'math', | ||
protoPath: join(__dirname, './../src/grpc/math.proto') | ||
}, | ||
}); | ||
await app.startAllMicroservicesAsync(); | ||
await app.init(); | ||
}); | ||
|
||
it(`/POST`, () => { | ||
return request(server) | ||
.post('/') | ||
.send([1, 2, 3, 4, 5]) | ||
.expect(200, { result: '15' }); | ||
}); | ||
|
||
afterEach(async () => { | ||
await app.close(); | ||
}); | ||
}); |
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,38 @@ | ||
import { Controller, Post, Body, HttpCode } from '@nestjs/common'; | ||
import { | ||
Client, | ||
MessagePattern, | ||
ClientProxy, | ||
Transport, | ||
GrpcRoute, | ||
ClientGrpc, | ||
} from '@nestjs/microservices'; | ||
import { Observable } from 'rxjs/Observable'; | ||
import { of } from 'rxjs/observable/of'; | ||
import { join } from 'path'; | ||
|
||
@Controller() | ||
export class GrpcController { | ||
@Client({ | ||
transport: Transport.GRPC, | ||
options: { | ||
package: 'math', | ||
protoPath: join(__dirname, 'math.proto'), | ||
} | ||
}) | ||
client: ClientGrpc; | ||
|
||
@Post() | ||
@HttpCode(200) | ||
call(@Body() data: number[]): Observable<number> { | ||
const svc = this.client.getService<any>('Math'); | ||
return svc.Sum(data); | ||
} | ||
|
||
@GrpcRoute('Math', 'Sum') | ||
async sum({ data }: { data: number[] }): Promise<any> { | ||
return of({ | ||
result: data.reduce((a, b) => a + b), | ||
}); | ||
} | ||
} |
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,15 @@ | ||
syntax = "proto3"; | ||
|
||
package math; | ||
|
||
service Math { | ||
rpc Sum (RequestSum) returns (SumResult) {} | ||
} | ||
|
||
message SumResult { | ||
required int32 result = 1; | ||
} | ||
|
||
message RequestSum { | ||
repeated int32 data = 1; | ||
} |
27 changes: 27 additions & 0 deletions
27
integration/microservices/src/mqtt/mqtt-multicast.controller.ts
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 { Controller, Get } from '@nestjs/common'; | ||
import { | ||
Client, | ||
MessagePattern, | ||
ClientProxy, | ||
Transport, | ||
} from '@nestjs/microservices'; | ||
import { Observable } from 'rxjs/Observable'; | ||
import { scan, take } from 'rxjs/operators'; | ||
|
||
@Controller() | ||
export class MqttMulticastController { | ||
@Client({ transport: Transport.MQTT }) | ||
client: ClientProxy; | ||
|
||
@Get('multicast') | ||
multicats() { | ||
return this.client | ||
.send<number>({ cmd: 'multicast' }, {}) | ||
.pipe(scan((a, b) => a + b), take(2)); | ||
} | ||
|
||
@MessagePattern({ cmd: 'multicast' }) | ||
replyMulticast(): Observable<number> { | ||
return new Observable(observer => observer.next(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
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 |
---|---|---|
@@ -1,25 +1,25 @@ | ||
export declare const metadata: { | ||
MODULES: string; | ||
IMPORTS: string; | ||
COMPONENTS: string; | ||
PROVIDERS: string; | ||
CONTROLLERS: string; | ||
EXPORTS: string; | ||
MODULES: string; | ||
IMPORTS: string; | ||
COMPONENTS: string; | ||
PROVIDERS: string; | ||
CONTROLLERS: string; | ||
EXPORTS: string; | ||
}; | ||
export declare const SHARED_MODULE_METADATA = "__sharedModule__"; | ||
export declare const GLOBAL_MODULE_METADATA = "__globalModule__"; | ||
export declare const PATH_METADATA = "path"; | ||
export declare const PARAMTYPES_METADATA = "design:paramtypes"; | ||
export declare const SELF_DECLARED_DEPS_METADATA = "self:paramtypes"; | ||
export declare const METHOD_METADATA = "method"; | ||
export declare const ROUTE_ARGS_METADATA = "__routeArguments__"; | ||
export declare const CUSTOM_ROUTE_AGRS_METADATA = "__customRouteArgs__"; | ||
export declare const EXCEPTION_FILTERS_METADATA = "__exceptionFilters__"; | ||
export declare const FILTER_CATCH_EXCEPTIONS = "__filterCatchExceptions__"; | ||
export declare const PIPES_METADATA = "__pipes__"; | ||
export declare const GUARDS_METADATA = "__guards__"; | ||
export declare const RENDER_METADATA = "__renderTemplate__"; | ||
export declare const INTERCEPTORS_METADATA = "__interceptors__"; | ||
export declare const HTTP_CODE_METADATA = "__httpCode__"; | ||
export declare const GATEWAY_MIDDLEWARES = "__gatewayMiddlewares"; | ||
export declare const MODULE_PATH = "__module_path__"; | ||
export declare const SHARED_MODULE_METADATA = '__sharedModule__'; | ||
export declare const GLOBAL_MODULE_METADATA = '__globalModule__'; | ||
export declare const PATH_METADATA = 'path'; | ||
export declare const PARAMTYPES_METADATA = 'design:paramtypes'; | ||
export declare const SELF_DECLARED_DEPS_METADATA = 'self:paramtypes'; | ||
export declare const METHOD_METADATA = 'method'; | ||
export declare const ROUTE_ARGS_METADATA = '__routeArguments__'; | ||
export declare const CUSTOM_ROUTE_AGRS_METADATA = '__customRouteArgs__'; | ||
export declare const EXCEPTION_FILTERS_METADATA = '__exceptionFilters__'; | ||
export declare const FILTER_CATCH_EXCEPTIONS = '__filterCatchExceptions__'; | ||
export declare const PIPES_METADATA = '__pipes__'; | ||
export declare const GUARDS_METADATA = '__guards__'; | ||
export declare const RENDER_METADATA = '__renderTemplate__'; | ||
export declare const INTERCEPTORS_METADATA = '__interceptors__'; | ||
export declare const HTTP_CODE_METADATA = '__httpCode__'; | ||
export declare const GATEWAY_MIDDLEWARES = '__gatewayMiddlewares'; | ||
export declare const MODULE_PATH = '__module_path__'; |
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
Oops, something went wrong.