Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
ClientGrpc,
} from '@nestjs/microservices';
import { Observable, Subject } from 'rxjs';
import { grpcClientOptions } from '../grpc-client.options';
import { HeroById } from './interfaces/hero-by-id.interface';
import { HeroByName } from './interfaces/hero-by-name.interface';
import { Hero, HeroList } from './interfaces/hero.interface';
interface HeroService {
getHeroById(data: { id: number }): Observable;
listHeroesByName(data: { name: string }): Observable;
}
@Controller()
export class HeroController implements OnModuleInit {
@Client(grpcClientOptions) private readonly client: ClientGrpc;
private heroService: HeroService;
private items = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Doe' },
{ id: 3, name: 'Billy' },
{ id: 4, name: 'Joey' },
];
onModuleInit() {
this.heroService = this.client.getService('HeroService');
}
// http methods
@Get()
call(): Observable {
async function bootstrap() {
const HTTP_PORT = parseInt(process.env.HTTP_PORT, 10) || 3000; // Default to 3000
const GRPC_PORT = parseInt(process.env.GRPC_PORT, 10) || 4000; // Default to 4000
const app = await NestFactory.create(NotificationModule);
app.connectMicroservice({
transport: Transport.GRPC,
options: {
url: `0.0.0.0:${GRPC_PORT}`,
package: 'notification',
protoPath: join(__dirname, 'src/grpc/notification.proto'),
},
});
app.connectMicroservice({
transport: Transport.REDIS,
options: {
url: process.env.REDIS_HOST,
},
});
await app.startAllMicroservicesAsync();
await app.listen(HTTP_PORT);
private createClient(
options: CheckGRPCServiceOptions,
): NestJSMicroservices.ClientGrpc {
const {
timeout,
healthServiceName,
healthServiceCheck,
...grpcOptions
} = options;
// TODO: Remove `as any` as soon nestjs/nest#2240 gets merged
return this.nestJsMicroservices.ClientProxyFactory.create({
transport: Transport.GRPC,
options: grpcOptions as GrpcOptionsOptions,
}) as any;
}
async function bootstrap() {
const HTTP_PORT = parseInt(process.env.HTTP_PORT, 10) || 3000; // Default to 3000
const GRPC_PORT = parseInt(process.env.GRPC_PORT, 10) || 4000; // Default to 4000
const app = await NestFactory.create(AllianceModule);
app.connectMicroservice({
transport: Transport.GRPC,
options: {
url: `0.0.0.0:${GRPC_PORT}`,
package: 'alliance',
protoPath: join(__dirname, 'src/grpc/alliance.proto'),
},
});
await app.startAllMicroservicesAsync();
await app.listen(HTTP_PORT);
}
async function bootstrap() {
const HTTP_PORT = parseInt(process.env.HTTP_PORT, 10) || 3000; // Default to 3000
const GRPC_PORT = parseInt(process.env.GRPC_PORT, 10) || 4000; // Default to 4000
const app = await NestFactory.create(FollowModule);
app.connectMicroservice({
transport: Transport.GRPC,
options: {
url: `0.0.0.0:${GRPC_PORT}`,
package: 'follow',
protoPath: join(__dirname, 'src/grpc/follow.proto'),
},
});
await app.startAllMicroservicesAsync();
await app.listen(HTTP_PORT);
}
async function bootstrap() {
const HTTP_PORT = parseInt(process.env.HTTP_PORT, 10) || 3000; // Default to 3000
const GRPC_PORT = parseInt(process.env.GRPC_PORT, 10) || 4000; // Default to 4000
const app = await NestFactory.create(CommentModule);
app.connectMicroservice({
transport: Transport.GRPC,
options: {
url: `0.0.0.0:${GRPC_PORT}`,
package: 'post',
protoPath: join(__dirname, 'src/grpc/post.proto'),
},
});
await app.startAllMicroservicesAsync();
await app.listen(HTTP_PORT);
}
async function bootstrap() {
const HTTP_PORT = parseInt(process.env.HTTP_PORT, 10) || 3000; // Default to 3000
const GRPC_PORT = parseInt(process.env.GRPC_PORT, 10) || 4000; // Default to 4000
const app = await NestFactory.create(HashtagModule);
app.connectMicroservice({
transport: Transport.GRPC,
options: {
url: `0.0.0.0:${GRPC_PORT}`,
package: 'hashtag',
protoPath: join(__dirname, 'src/grpc/hashtag.proto'),
},
});
await app.startAllMicroservicesAsync();
await app.listen(HTTP_PORT);
}
async function bootstrap() {
const HTTP_PORT = parseInt(process.env.HTTP_PORT, 10) || 3000; // Default to 3000
const GRPC_PORT = parseInt(process.env.GRPC_PORT, 10) || 4000; // Default to 4000
const app = await NestFactory.create(CorporationModule);
app.connectMicroservice({
transport: Transport.GRPC,
options: {
url: `0.0.0.0:${GRPC_PORT}`,
package: 'corporation',
protoPath: join(__dirname, 'src/grpc/corporation.proto'),
},
});
await app.startAllMicroservicesAsync();
await app.listen(HTTP_PORT);
}
bootstrap();
const HTTP_PORT = parseInt(process.env.HTTP_PORT, 10) || 3000; // Default to 3000
const GRPC_PORT = parseInt(process.env.GRPC_PORT, 10) || 4000; // Default to 4000
const app = await NestFactory.create(NotificationModule);
app.connectMicroservice({
transport: Transport.GRPC,
options: {
url: `0.0.0.0:${GRPC_PORT}`,
package: 'notification',
protoPath: join(__dirname, 'src/grpc/notification.proto'),
},
});
app.connectMicroservice({
transport: Transport.REDIS,
options: {
url: process.env.REDIS_HOST,
},
});
await app.startAllMicroservicesAsync();
await app.listen(HTTP_PORT);
}
}
// grpc streaming methods
@GrpcStreamMethod('HeroService')
async getHeroByIdStream(messages: Observable): Promise {
return new Promise((resolve, reject) => {
messages.subscribe(msg => {
resolve(this.items.find(({ id }) => id === msg.id));
}, err => {
reject(err);
});
});
}
@GrpcStreamMethod('HeroService')
async listHeroesByNameStream(messages: Observable): Promise {
return new Promise((resolve, reject) => {
messages.subscribe(msg => {
resolve({ heroes: this.items.filter(({ name }) => name.startsWith(msg.name)) });
}, err => {
reject(err);
});
});
}
}