Skip to content

Commit 198d270

Browse files
sleipnirpolvalente
andauthored
More didactic documentation (#356)
* More didactic documentation * Some adjusts * Added pontuation * Added link * Added protobuf compilation example * Apply suggestions from code review --------- Co-authored-by: Paulo Valente <16843419+polvalente@users.noreply.github.com>
1 parent f8c8b17 commit 198d270

File tree

1 file changed

+123
-8
lines changed

1 file changed

+123
-8
lines changed

README.md

Lines changed: 123 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ An Elixir implementation of [gRPC](http://www.grpc.io/).
1313

1414
- [Installation](#installation)
1515
- [Usage](#usage)
16+
- [Simple RPC](#simple-rpc)
17+
- [HTTP Transcoding](#http-transcoding)
18+
- [Start Application](#start-application)
1619
- [Features](#features)
1720
- [Benchmark](#benchmark)
1821
- [Contributing](#contributing)
@@ -35,9 +38,42 @@ The package can be installed as:
3538

3639
## Usage
3740

38-
1. Generate Elixir code from proto file as [protobuf-elixir](https://github.com/tony612/protobuf-elixir#usage) shows(especially the `gRPC Support` section).
41+
1. Write your protobuf file:
3942

40-
2. Implement the server side code like below and remember to return the expected message types.
43+
```protobuf
44+
syntax = "proto3";
45+
46+
package helloworld;
47+
48+
// The request message containing the user's name.
49+
message HelloRequest {
50+
string name = 1;
51+
}
52+
53+
// The response message containing the greeting
54+
message HelloReply {
55+
string message = 1;
56+
}
57+
58+
// The greeting service definition.
59+
service Greeter {
60+
// Greeting function
61+
rpc SayHello (HelloRequest) returns (HelloReply) {}
62+
}
63+
64+
```
65+
66+
2. Then generate Elixir code from proto file as [protobuf-elixir](https://github.com/tony612/protobuf-elixir#usage) shows (especially the `gRPC Support` section) or using [protobuf_generate](https://hex.pm/packages/protobuf_generate) hex package. Example using `protobuf_generate` lib:
67+
68+
```shell
69+
mix protobuf.generate --output-path=./lib --include-path=./priv/protos helloworld.proto
70+
```
71+
72+
In the following sections you will see how to implement gRPC server logic.
73+
74+
### **Simple RPC**
75+
76+
1. Implement the server side code like below and remember to return the expected message types.
4177

4278
```elixir
4379
defmodule Helloworld.Greeter.Server do
@@ -50,9 +86,7 @@ defmodule Helloworld.Greeter.Server do
5086
end
5187
```
5288

53-
3. Start the server
54-
55-
You can start the gRPC server as a supervised process. First, add `GRPC.Server.Supervisor` to your supervision tree.
89+
2. Define gRPC endpoints
5690

5791
```elixir
5892
# Define your endpoint
@@ -62,7 +96,86 @@ defmodule Helloworld.Endpoint do
6296
intercept GRPC.Server.Interceptors.Logger
6397
run Helloworld.Greeter.Server
6498
end
99+
```
100+
101+
We will use this module [in the gRPC server startup section](#start-application).
102+
103+
**__Note:__** For other types of RPC call like streams see [here](interop/lib/interop/server.ex).
104+
105+
### **HTTP Transcoding**
106+
107+
1. Adding [grpc-gateway annotations](https://cloud.google.com/endpoints/docs/grpc/transcoding) to your protobuf file definition:
108+
109+
```protobuf
110+
import "google/api/annotations.proto";
111+
import "google/protobuf/timestamp.proto";
112+
113+
package helloworld;
114+
115+
// The greeting service definition.
116+
service Greeter {
117+
// Sends a greeting
118+
rpc SayHello (HelloRequest) returns (HelloReply) {
119+
option (google.api.http) = {
120+
get: "/v1/greeter/{name}"
121+
};
122+
}
123+
124+
rpc SayHelloFrom (HelloRequestFrom) returns (HelloReply) {
125+
option (google.api.http) = {
126+
post: "/v1/greeter"
127+
body: "*"
128+
};
129+
}
130+
}
131+
```
65132

133+
2. Add protoc plugin dependency and compile your protos using [protobuf_generate](https://github.com/drowzy/protobuf_generate) hex [package](https://hex.pm/packages/protobuf_generate):
134+
135+
In mix.exs:
136+
137+
```elixir
138+
def deps do
139+
[
140+
{:grpc, "~> 0.7"},
141+
{:protobuf_generate, "~> 0.1.1"}
142+
]
143+
end
144+
```
145+
146+
And in your terminal:
147+
148+
```shell
149+
mix protobuf.generate \
150+
--include-path=priv/proto \
151+
--include-path=deps/googleapis \
152+
--generate-descriptors=true \
153+
--output-path=./lib \
154+
--plugins=ProtobufGenerate.Plugins.GRPCWithOptions \
155+
google/api/annotations.proto google/api/http.proto helloworld.proto
156+
```
157+
158+
3. Enable http_transcode option in your Server module
159+
```elixir
160+
defmodule Helloworld.Greeter.Server do
161+
use GRPC.Server,
162+
service: Helloworld.Greeter.Service,
163+
http_transcode: true
164+
165+
@spec say_hello(Helloworld.HelloRequest.t, GRPC.Server.Stream.t) :: Helloworld.HelloReply.t
166+
def say_hello(request, _stream) do
167+
%Helloworld.HelloReply{message: "Hello #{request.name}"}
168+
end
169+
end
170+
```
171+
172+
See full application code in [helloworld_transcoding](examples/helloworld_transcoding) example.
173+
174+
### **Start Application**
175+
176+
1. Start gRPC Server in your supervisor tree or Application module:
177+
178+
```elixir
66179
# In the start function of your Application
67180
defmodule HelloworldApp do
68181
use Application
@@ -78,7 +191,7 @@ defmodule HelloworldApp do
78191
end
79192
```
80193

81-
4. Call rpc:
194+
2. Call rpc:
82195

83196
```elixir
84197
iex> {:ok, channel} = GRPC.Stub.connect("localhost:50051")
@@ -90,7 +203,7 @@ iex> {:ok, channel} = GRPC.Stub.connect("localhost:50051", interceptors: [GRPC.C
90203
...
91204
```
92205

93-
Check [examples](examples) and [interop](interop)(Interoperability Test) for some examples.
206+
Check the [examples](examples) and [interop](interop) directories in the project's source code for some examples.
94207

95208
## Features
96209

@@ -99,11 +212,13 @@ Check [examples](examples) and [interop](interop)(Interoperability Test) for som
99212
- [Server-streaming](https://grpc.io/docs/what-is-grpc/core-concepts/#server-streaming-rpc)
100213
- [Client-streaming](https://grpc.io/docs/what-is-grpc/core-concepts/#client-streaming-rpc)
101214
- [Bidirectional-streaming](https://grpc.io/docs/what-is-grpc/core-concepts/#bidirectional-streaming-rpc)
215+
- [HTTP Transcoding](https://cloud.google.com/endpoints/docs/grpc/transcoding)
102216
- [TLS Authentication](https://grpc.io/docs/guides/auth/#supported-auth-mechanisms)
103217
- [Error handling](https://grpc.io/docs/guides/error/)
104-
- Interceptors(See [`GRPC.Endpoint`](https://github.com/elixir-grpc/grpc/blob/master/lib/grpc/endpoint.ex))
218+
- Interceptors (See [`GRPC.Endpoint`](https://github.com/elixir-grpc/grpc/blob/master/lib/grpc/endpoint.ex))
105219
- [Connection Backoff](https://github.com/grpc/grpc/blob/master/doc/connection-backoff.md)
106220
- Data compression
221+
- [gRPC Reflection](https://github.com/elixir-grpc/grpc-reflection)
107222

108223
## Benchmark
109224

0 commit comments

Comments
 (0)