loopback-next icon indicating copy to clipboard operation
loopback-next copied to clipboard

gRPC api : creating gRPC controller using loopback4 for an already existing rest api

Open Jyoti-13 opened this issue 2 years ago • 7 comments

In this pull request, I am proposing a solution for creating a gRPC controller for an existing rest api.

How to review this pull request:

I have created an audit.proto file that provides a service to implement a request and response sent by a service.

Steps that I have followed:

  1. Created a proto file, added service and created a request and response method.
  2. Compiled proto file (audit.proto) into typescript file (audit.proto.ts).
  3. Created a controller (audit.controller.ts) with implementation of proto service methods.
  4. Added gRPC server details in application.ts.

Queries and Concerns:

  • Is there any better approach?
  • Difficulty in creating message responses in a proto file for complex data.
  • Difficulty in using predefined data types in proto file for example: for date we need to use timestamp.proto for which first it has to be compiled into typescript file manually.

Next, read through the code to see implementation details you are interested in:

  • https://github.com/Jyoti-13/loopback-next/tree/grpc-microservice/sandbox/example/grpc-rest-server is created to show the proposed usage in user applications
  • https://www.npmjs.com/package/loopback-grpc shows how to use gRPC server in Loopback 4.
  • https://developers.google.com/protocol-buffers/docs/proto3 This guide describes how to use the protocol buffer language to structure your protocol buffer data.

Jyoti-13 avatar Dec 07 '21 09:12 Jyoti-13

@dhmlau @achrinza @raymondfeng we would like to get your inputs on this one. We want to try getting a stable and scalable approach here. This can be pretty significant extension actually if done correctly. Especially for microservices architecture.

samarpan-b avatar Dec 07 '21 09:12 samarpan-b

Pull Request Test Coverage Report for Build 1548666677

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 76.034%

Totals Coverage Status
Change from base Build 1533140642: 0%
Covered Lines: 16923
Relevant Lines: 17910

💛 - Coveralls

coveralls avatar Dec 08 '21 07:12 coveralls

Thanks for the PR, I've skimmed through the code but there doesn't seem to be any REST Application being created. Is there a line of code you could point me to where the REST and gRPC are "merged" together?

achrinza avatar Dec 08 '21 08:12 achrinza

Thanks for the PR, I've skimmed through the code but there doesn't seem to be any REST Application being created. Is there a line of code you could point me to where the REST and gRPC are "merged" together?

@achrinza I have updated the code, gRPC and REST are merged together in index.ts.

Jyoti-13 avatar Dec 09 '21 12:12 Jyoti-13

Thanks for the changes, @Jyoti-13. Based on the code, I don't think I fully understand the intended objective(s) of this PR.

From the PR's body:

a solution for creating a gRPC controller for an existing rest api.

My understanding is to allow gRPC and REST servers to run simultaneously. However, the current code seems to only run either gRPC or REST, not both at the same time.

Would you be able to clarify this? Thanks

achrinza avatar Jan 07 '22 12:01 achrinza

Thanks for the response, @achrinza. The user will decide which server should be used, gRPC or REST. I am facing issues in converting REST api to gRPC:-

  • for every controller function we have to create proto file manually which is time consuming.
  • if we have many entities in our database then the proto file will be more complex.
  • if the datatype is complex eg. date or any.. then we have to import predefined proto files for these datatypes and also compile them into .ts file before using them in the code.

Also you can check queries and concerns in the description.

Jyoti-13 avatar Jan 12 '22 11:01 Jyoti-13

@dhmlau @achrinza @raymondfeng inputs from you will be really appreciated.

yeshamavani avatar Nov 01 '22 06:11 yeshamavani