You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `@loopback/grpc` component enables LoopBack 4 as a [gRPC](https://grpc.io/) Server. Also it provides with a gRPC decorator to define your RPC Method implementations from your Application Controllers.
8
8
9
+
### Features
10
+
11
+
- Handles unary, client streaming, server streaming and bidirectional streaming calls
12
+
- Provides options for TLS and mTLS
13
+
9
14
## Installation
10
15
11
16
Install the `@loopback/grpc` component in your LoopBack 4 Application.
// Bind GreeterController to the LoopBack Application
39
+
// only if your Boot Mixins do not load this directory
40
+
// see https://loopback.io/doc/en/lb4/Booting-an-Application.html
41
+
// app.controller(GreeterController);
35
42
// Start App
36
43
app.start();
37
44
```
38
45
39
-
## Grpc auto-generated code
46
+
## gRPC auto-generated code
40
47
41
48
The `@loopback/grpc` extension provides you with auto-generated interfaces and configurations for strict development.
42
49
@@ -46,71 +53,196 @@ Example:
46
53
47
54
```sh
48
55
- app
56
+
| - protos
57
+
|| - greeter.proto
58
+
| - protos-ts
49
59
| - controllers
50
-
|| - greeter
51
-
||| - greeter.proto
52
-
||| - greeter.ctrl.ts
60
+
|| - greeter.controller.ts
53
61
```
54
62
55
63
Once you start your app for first time it will automatically create your typescript interfaces from the `greeter.proto` file.
56
64
57
65
```sh
58
66
- app
67
+
| - protos
68
+
|| - greeter.proto
69
+
| - protos-ts
70
+
|| - greeter.ts <--- Auto-generated
59
71
| - controllers
60
-
|| - greeter
61
-
||| - greeter.proto
62
-
||| - greeter.proto.ts <--- Auto-generated
63
-
||| - greeter.ctrl.ts
72
+
|| - greeter.controller.ts
64
73
```
65
74
66
75
Once your interfaces and configurations are created, you can start building your controller logic.
67
76
68
-
## Grpc Controller
77
+
NB: you can also manually generate your interfaces from \_.proto files by creating a `.js` file at the root of your project like below. A good practice would be to add the directory containing your generated `.ts` files to `.gitignore` and to add `node generate.js` to the command `npm run build`.
69
78
70
-
The `@loopback/grpc` component provides you with a handy decorator to implement GRPC Methods within your LoopBack controllers.
console.log(formatWithColor('Generating proto.ts files from *.proto...', 33));
107
+
generator.execute();
108
+
console.log(formatWithColor('All proto.ts files have been generated', 32));
109
+
```
110
+
111
+
## gRPC Controller
112
+
113
+
The `@loopback/grpc` component provides you with a handy decorator to implement gRPC Methods within your LoopBack controllers. The decorator will automatically map the correct calls from the file descriptor, the method name and the controller name. If you want an other suffix than `(Ctrl|Controller)`, you can use the argument `controllerNameRegex`.
114
+
115
+
`app/controllers/greeter.controller.ts`
116
+
117
+
```ts
118
+
import {
119
+
TestRequest,
120
+
TestResponse,
121
+
Greeter,
122
+
protoMetadata,
123
+
} from'../protos-ts/greeter';
124
+
125
+
classGreeterCtrlimplementsGreeter {
126
+
// Tell LoopBack that this is a Service RPC implementation
Decorators provide annotations for class methods and arguments. Decorators use the form `@decorator` where `decorator` is the name of the function that will be called at runtime.
@@ -8,51 +7,62 @@ Decorators provide annotations for class methods and arguments. Decorators use t
8
7
This decorator allows you to annotate a `Controller` class. The decorator will setup a GRPC Service.
9
8
10
9
**Example**
11
-
````js
10
+
11
+
```js
12
12
/**
13
-
* Setup gRPC MicroService
14
-
**/
15
-
//myproject/controllers/greeter/Greeter.ts
16
-
//myproject/controllers/greeter/greeter.proto
17
-
//myproject/controllers/greeter/greeter.proto.ts
13
+
* Setup gRPC MicroService
14
+
**/
15
+
//myproject/controllers/greeter.controller.ts
16
+
//myproject/protos/greeter/greeter.proto
17
+
//myproject/protos-ts/greeter/greeter.ts
18
18
//Note: greeter.proto.ts is automatically generated from
0 commit comments