Skip to content

Commit 2a90af1

Browse files
authored
Docs fixes: unary response correction, a note on adapter_opts.cred (#484)
* docs: Request, not Reply * add documentation on how to set up TLS Points one to the GRPC.Credential module, notes the key in adapter_opts to use. The documentation that would have saved me 15 minutes of reading source code. * link into grpc_client now that it has been moved there * expand the server quickstart guide with supervision, cred, and some words on what endpoints are
1 parent 8e144de commit 2a90af1

4 files changed

Lines changed: 73 additions & 17 deletions

File tree

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ iex> GRPC.Stream.from([1, 2])
205205
...> {:error, {:exception, _reason}} ->
206206
...> {:error, GRPC.RPCError.exception(message: "Booomm")}
207207
...> end)
208-
```
208+
```
209209

210210
In this example:
211211

@@ -233,7 +233,7 @@ def say_unary_hello(request, _materializer) do
233233

234234
{:error, reason} ->
235235
{:error, GRPC.RPCError.exception(message: "error calling external process: #{inspect(reason)}")}
236-
236+
237237
error ->
238238
Logger.error("Unknown error")
239239
error
@@ -297,12 +297,12 @@ children = [
297297

298298
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
299299
Supervisor.start_link(children, opts)
300-
```
300+
```
301301

302302
You can also start it manually in scripts or test environments:
303303
```elixir
304304
{:ok, _pid} = DynamicSupervisor.start_link(strategy: :one_for_one, name: GRPC.Client.Supervisor)
305-
```
305+
```
306306

307307
Then connect with gRPC server:
308308

@@ -331,7 +331,7 @@ iex> {:ok, reply} = channel |> Helloworld.GreetingServer.Stub.say_unary_hello(re
331331

332332
## Target Schemes and Resolvers
333333

334-
The `connect/2` function supports URI-like targets that are resolved via the internal **gRPC** [Resolver](lib/grpc/client/resolver.ex).
334+
The `connect/2` function supports URI-like targets that are resolved via the internal **gRPC** [Resolver](grpc_client/lib/grpc/client/resolver.ex).
335335
You can connect using `DNS`, `Unix Domain sockets`, `IPv4/IPv6`, or even `xDS-based endpoints`.
336336

337337
### Supported formats:
@@ -377,7 +377,7 @@ iex> {:ok, channel} =
377377

378378
## Client Adapters
379379

380-
By default, `GRPC.Stub.connect/2` uses the **Gun** adapter.
380+
By default, `GRPC.Stub.connect/2` uses the **Gun** adapter.
381381
You can switch to **Mint** (pure Elixir HTTP/2) or other adapters as needed.
382382

383383
### Using Mint Adapter

grpc_client/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ children = [
9191

9292
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
9393
Supervisor.start_link(children, opts)
94-
```
94+
```
9595

9696
You can also start it manually in scripts or test environments:
9797
```elixir
9898
{:ok, _pid} = DynamicSupervisor.start_link(strategy: :one_for_one, name: GRPC.Client.Supervisor)
99-
```
99+
```
100100

101101
Then connect with gRPC server:
102102

@@ -125,7 +125,7 @@ iex> {:ok, reply} = channel |> Helloworld.GreetingServer.Stub.say_unary_hello(re
125125

126126
## Target Schemes and Resolvers
127127

128-
The `connect/2` function supports URI-like targets that are resolved via the internal **gRPC** [Resolver](lib/grpc/client/resolver.ex).
128+
The `connect/2` function supports URI-like targets that are resolved via the internal **gRPC** [Resolver](grpc_client/lib/grpc/client/resolver.ex).
129129
You can connect using `DNS`, `Unix Domain sockets`, `IPv4/IPv6`, or even `xDS-based endpoints`.
130130

131131
### Supported formats:
@@ -171,7 +171,7 @@ iex> {:ok, channel} =
171171

172172
## Client Adapters
173173

174-
By default, `GRPC.Stub.connect/2` uses the **Gun** adapter.
174+
By default, `GRPC.Stub.connect/2` uses the **Gun** adapter.
175175
You can switch to **Mint** (pure Elixir HTTP/2) or other adapters as needed.
176176

177177
### Using Mint Adapter

grpc_server/README.md

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ defmodule HelloworldStreams.Server do
9898
def say_unary_hello(request, materializer) do
9999
request
100100
|> GRPC.Stream.unary(materializer: materializer)
101-
|> GRPC.Stream.map(fn %HelloReply{} = reply ->
101+
|> GRPC.Stream.map(fn %HelloRequest{} = reply ->
102102
%HelloReply{message: "[Reply] #{reply.message}"}
103103
end)
104104
|> GRPC.Stream.run()
@@ -195,7 +195,7 @@ iex> GRPC.Stream.from([1, 2])
195195
...> {:error, {:exception, _reason}} ->
196196
...> {:error, GRPC.RPCError.exception(message: "Booomm")}
197197
...> end)
198-
```
198+
```
199199

200200
In this example:
201201

@@ -223,7 +223,7 @@ def say_unary_hello(request, _materializer) do
223223

224224
{:error, reason} ->
225225
{:error, GRPC.RPCError.exception(message: "error calling external process: #{inspect(reason)}")}
226-
226+
227227
error ->
228228
Logger.error("Unknown error")
229229
error
@@ -269,6 +269,20 @@ defmodule Helloworld.Application do
269269
end
270270
```
271271

272+
A TLS configuration can be defined by assigning a `%GRPC.Credential{}` struct, typically created by calling
273+
`GRPC.Credential.new/1`, to the `cred` key in the `adapter_opts` option. For example:
274+
275+
```elixir
276+
{
277+
GRPC.Server.Supervisor, [
278+
endpoint: Helloworld.Endpoint,
279+
port: 50051,
280+
start_server: true,
281+
adapter_opts: [cred: GRPC.Credential.new(ssl: Application.get_env(:my_app, :ssl))]
282+
]
283+
}
284+
```
285+
272286
# Client Usage
273287

274288
This section demonstrates how to establish client connections and perform RPC calls using the Elixir gRPC client.
@@ -287,12 +301,12 @@ children = [
287301

288302
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
289303
Supervisor.start_link(children, opts)
290-
```
304+
```
291305

292306
You can also start it manually in scripts or test environments:
293307
```elixir
294308
{:ok, _pid} = DynamicSupervisor.start_link(strategy: :one_for_one, name: GRPC.Client.Supervisor)
295-
```
309+
```
296310

297311
Then connect with gRPC server:
298312

@@ -321,7 +335,7 @@ iex> {:ok, reply} = channel |> Helloworld.GreetingServer.Stub.say_unary_hello(re
321335

322336
## Target Schemes and Resolvers
323337

324-
The `connect/2` function supports URI-like targets that are resolved via the internal **gRPC** [Resolver](lib/grpc/client/resolver.ex).
338+
The `connect/2` function supports URI-like targets that are resolved via the internal **gRPC** [Resolver](grpc_client/lib/grpc/client/resolver.ex).
325339
You can connect using `DNS`, `Unix Domain sockets`, `IPv4/IPv6`, or even `xDS-based endpoints`.
326340

327341
### Supported formats:
@@ -367,7 +381,7 @@ iex> {:ok, channel} =
367381

368382
## Client Adapters
369383

370-
By default, `GRPC.Stub.connect/2` uses the **Gun** adapter.
384+
By default, `GRPC.Stub.connect/2` uses the **Gun** adapter.
371385
You can switch to **Mint** (pure Elixir HTTP/2) or other adapters as needed.
372386

373387
### Using Mint Adapter

grpc_server/guides/getting_started/quickstart.livemd

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ end
101101

102102
## Endpoint with Interceptor
103103

104+
Endpoints allow starting one or more servers via the `run` directive along with
105+
optional middleware which are registered using the `intercept` directive.
106+
104107
```elixir
105108
defmodule HelloEndpoint do
106109
use GRPC.Endpoint
@@ -110,6 +113,9 @@ defmodule HelloEndpoint do
110113
end
111114
```
112115

116+
Interceptors are called in the order they appear, allowing for pluggable logging,
117+
authentication, etc. to be applied to incoming requests.
118+
113119
---
114120

115121
## Starting the Server
@@ -123,6 +129,42 @@ Here we start the GRPC server under supervision at port `50051`.
123129
IO.puts("gRPC Server running on port 50051")
124130
```
125131

132+
A TLS configuration can be defined by assigning a `%GRPC.Credential{}` struct, typically created by calling
133+
`GRPC.Credential.new/1`, to the `cred` key in the `adapter_opts` option. For example:
134+
135+
```elixir
136+
adapter_opts: [cred: GRPC.Credential.new(ssl: Application.get_env(:my_app, :ssl))]
137+
```
138+
139+
### As part of a supervision tree
140+
141+
Typically the server will be started as part of a supervision tree rather than started
142+
manually:
143+
144+
```elixir
145+
defmodule Helloworld.Application do
146+
@moduledoc false
147+
use Application
148+
149+
@impl true
150+
def start(_type, _args) do
151+
children = [
152+
{
153+
GRPC.Server.Supervisor, [
154+
endpoint: HelloEndpoint,
155+
port: 50051,
156+
start_server: true,
157+
adapter_opts: [cred: GRPC.Credential.new(ssl: Application.get_env(:my_app, :ssl))]
158+
]
159+
}
160+
]
161+
162+
opts = [strategy: :one_for_one, name: Helloworld.Supervisor]
163+
Supervisor.start_link(children, opts)
164+
end
165+
end
166+
```
167+
126168
---
127169

128170
## Create a Client and Test the RPC

0 commit comments

Comments
 (0)