Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/http transcoding (Continuation) #351

Merged
merged 75 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from 74 commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
6addbaa
minimal working example
drowzy Sep 8, 2022
55e9b71
return correct content-type when codec is JSON
drowzy Sep 8, 2022
fd09f3f
support default rpc options arg
drowzy Sep 8, 2022
f97bb44
add custom protoc plugin
drowzy Sep 9, 2022
6b068f7
include api extensions
drowzy Sep 9, 2022
4cab3b4
include http options in helloworld example
drowzy Sep 9, 2022
dc5c6de
dont pack message if the method was called as a transcode req
drowzy Sep 9, 2022
1248ab2
wip! build route from HttpRule
drowzy Sep 12, 2022
fb13316
update helloworld example with HttpRule options
drowzy Sep 12, 2022
a8e4473
rename mod to transcode
drowzy Sep 12, 2022
3b661c4
fix! match rpc options in stub
drowzy Sep 13, 2022
e0a3f6d
encode segments to path
drowzy Sep 13, 2022
2a45dfc
expose routes from server
drowzy Sep 13, 2022
0cda91d
init http/json transcode integration tests
drowzy Sep 13, 2022
9e343e4
merge path bindings with body
drowzy Sep 13, 2022
1e18a03
http rule template lexer / parse rename literal -> identifier
drowzy Sep 13, 2022
adf26ad
fix warnings
drowzy Sep 13, 2022
de8d798
fix! merge request params before decoding into struct
drowzy Sep 14, 2022
f9ff4ab
update example curl requests
drowzy Sep 14, 2022
e9747e9
Fix typo in filenames
sleipnir Sep 14, 2022
00588c6
minimal working example
drowzy Sep 8, 2022
0282563
return correct content-type when codec is JSON
drowzy Sep 8, 2022
0a34f69
support default rpc options arg
drowzy Sep 8, 2022
d128b00
add custom protoc plugin
drowzy Sep 9, 2022
d6efc8e
include api extensions
drowzy Sep 9, 2022
8f318cb
include http options in helloworld example
drowzy Sep 9, 2022
dbf4f34
dont pack message if the method was called as a transcode req
drowzy Sep 9, 2022
089460f
wip! build route from HttpRule
drowzy Sep 12, 2022
3e3c052
update helloworld example with HttpRule options
drowzy Sep 12, 2022
94c013c
rename mod to transcode
drowzy Sep 12, 2022
1679d3e
fix! match rpc options in stub
drowzy Sep 13, 2022
bedd74e
encode segments to path
drowzy Sep 13, 2022
0abb32f
expose routes from server
drowzy Sep 13, 2022
2056430
init http/json transcode integration tests
drowzy Sep 13, 2022
dd59b00
merge path bindings with body
drowzy Sep 13, 2022
5b12f34
http rule template lexer / parse rename literal -> identifier
drowzy Sep 13, 2022
f7eba63
fix warnings
drowzy Sep 13, 2022
d2c4738
fix! merge request params before decoding into struct
drowzy Sep 14, 2022
1a016a7
update example curl requests
drowzy Sep 14, 2022
898c1fe
include http method in __call_rpc__
drowzy Sep 14, 2022
2c4c554
add query decoder
drowzy Sep 15, 2022
6118f5c
include query string in request mapping
drowzy Sep 15, 2022
c36293b
map request using HttpRule.body
drowzy Sep 15, 2022
152a2bf
comply to request mapping rules by checking HttpRule.body param
drowzy Sep 15, 2022
f97122b
support newline delimeted json for transcoded server streams
drowzy Sep 16, 2022
96fb763
base generator on protobuf 0.11.0
drowzy Sep 19, 2022
fcca6ef
map transcoded response using HttpRule.response_body
drowzy Sep 19, 2022
82cc13e
support nested field paths in http paths
drowzy Sep 19, 2022
d8ffd42
Fix! parse sub-paths for variable assignment
drowzy Sep 20, 2022
690cbef
support calling grpc method with json if http_transcode == true
drowzy Sep 21, 2022
69ef24b
remove params from template parsing
drowzy Sep 21, 2022
d170bb8
add GRPC.Server transcode example
drowzy Sep 22, 2022
51b7c33
get requests can use accept header to find codec
drowzy Sep 23, 2022
431aedf
remove reference to HttpRule type
drowzy Oct 20, 2022
65e5a11
rm protoc plugin in favour of protobuf_generate
drowzy Oct 26, 2022
ee9b94d
add new cowboy router middleware
drowzy Oct 26, 2022
95a4540
move routing specific func. to GRPC.Server.Router
drowzy Oct 26, 2022
271768c
don't generate protos before tests
drowzy Oct 26, 2022
3957bf9
reset helloworld example
drowzy Oct 27, 2022
9acc0ab
add helloworld_transcode example
drowzy Oct 27, 2022
6a21790
include google api protos in tests only
drowzy Oct 27, 2022
cb6d617
map gRPC code -> http status
drowzy Nov 15, 2022
ead4eaf
Minor adjusts
sleipnir Feb 15, 2024
2ea2a78
Merge. Resolved conflicts
sleipnir Feb 15, 2024
64768a3
Adjusts
sleipnir Feb 15, 2024
e5479d1
Remove deprecated
sleipnir Feb 16, 2024
24bbf51
Fix. Assert payload
sleipnir Feb 16, 2024
8613a46
Remove deprecated and fix some tests
sleipnir Feb 16, 2024
9b8a4ca
Fix. status handler test
sleipnir Feb 16, 2024
d9b0a3e
Update lib/grpc/codec/json.ex
polvalente Mar 25, 2024
cf86ec0
Update lib/grpc/codec/json.ex
polvalente Mar 25, 2024
4bb2d71
Update lib/grpc/server.ex
polvalente Mar 25, 2024
e282654
Update lib/grpc/transport/http2.ex
polvalente Mar 25, 2024
c4861b6
Update lib/grpc/server/transcode.ex
polvalente Mar 25, 2024
2384242
chore: remove prometheus stray deps
polvalente Mar 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion examples/helloworld/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ $ mix run --no-halt
$ mix run priv/client.exs
```

## HTTP Transcoding

``` shell
# Say hello
curl -H 'Content-type: application/json' http://localhost:50051/v1/greeter/test

# Say hello from
curl -XPOST -H 'Content-type: application/json' -d '{"name": "test", "from": "anon"}' http://localhost:50051/v1/greeter
```

## Regenerate Elixir code from proto

1. Modify the proto `priv/protos/helloworld.proto`
Expand All @@ -26,8 +36,10 @@ $ mix run priv/client.exs
mix escript.install hex protobuf
```
4. Generate the code:

```shell
$ protoc -I priv/protos --elixir_out=plugins=grpc:./lib/ priv/protos/helloworld.proto
$ (cd ../../; mix build_protobuf_escript && mix escript.build)
$ protoc -I priv/protos --elixir_out=:./lib/ --grpc_elixir_out=./lib --plugin="../../deps/protobuf/protoc-gen-elixir" --plugin="../../protoc-gen-grpc_elixir" priv/protos/helloworld.proto
```

Refer to [protobuf-elixir](https://github.com/tony612/protobuf-elixir#usage) for more information.
Expand Down
20 changes: 15 additions & 5 deletions examples/helloworld/lib/helloworld.pb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ defmodule Helloworld.HelloRequest do
field :name, 1, type: :string
end

defmodule Helloworld.HelloRequestFrom do
@moduledoc false
use Protobuf, protoc_gen_elixir_version: "0.11.0", syntax: :proto3

field :name, 1, type: :string
field :from, 2, type: :string
end

defmodule Helloworld.HelloReply do
@moduledoc false
use Protobuf, protoc_gen_elixir_version: "0.11.0", syntax: :proto3
Expand All @@ -13,14 +21,16 @@ defmodule Helloworld.HelloReply do
field :today, 2, type: Google.Protobuf.Timestamp
end

defmodule Helloworld.Greeter.Service do
defmodule Helloworld.GetMessageRequest do
@moduledoc false
use GRPC.Service, name: "helloworld.Greeter", protoc_gen_elixir_version: "0.11.0"
use Protobuf, protoc_gen_elixir_version: "0.11.0", syntax: :proto3

rpc :SayHello, Helloworld.HelloRequest, Helloworld.HelloReply
field :name, 1, type: :string
end

defmodule Helloworld.Greeter.Stub do
defmodule Helloworld.Message do
@moduledoc false
use GRPC.Stub, service: Helloworld.Greeter.Service
use Protobuf, protoc_gen_elixir_version: "0.11.0", syntax: :proto3

field :text, 1, type: :string
end
65 changes: 65 additions & 0 deletions examples/helloworld/lib/helloworld.svc.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
defmodule Helloworld.Greeter.Service do
@moduledoc false

use GRPC.Service, name: "helloworld.Greeter", protoc_gen_elixir_version: "0.11.0"

rpc(:SayHello, Helloworld.HelloRequest, Helloworld.HelloReply, %{
http: %{
type: Google.Api.PbExtension,
value: %Google.Api.HttpRule{
__unknown_fields__: [],
additional_bindings: [],
body: "",
pattern: {:get, "/v1/greeter/{name}"},
response_body: "",
selector: ""
}
}
})

rpc(:SayHelloFrom, Helloworld.HelloRequestFrom, Helloworld.HelloReply, %{
http: %{
type: Google.Api.PbExtension,
value: %Google.Api.HttpRule{
__unknown_fields__: [],
additional_bindings: [],
body: "*",
pattern: {:post, "/v1/greeter"},
response_body: "",
selector: ""
}
}
})
end

defmodule Helloworld.Greeter.Stub do
@moduledoc false

use GRPC.Stub, service: Helloworld.Greeter.Service
end

defmodule Helloworld.Messaging.Service do
@moduledoc false

use GRPC.Service, name: "helloworld.Messaging", protoc_gen_elixir_version: "0.11.0"

rpc(:GetMessage, Helloworld.GetMessageRequest, Helloworld.Message, %{
http: %{
type: Google.Api.PbExtension,
value: %Google.Api.HttpRule{
__unknown_fields__: [],
additional_bindings: [],
body: "",
pattern: {:get, "/v1/{name=messages/*}"},
response_body: "",
selector: ""
}
}
})
end

defmodule Helloworld.Messaging.Stub do
@moduledoc false

use GRPC.Stub, service: Helloworld.Messaging.Service
end
25 changes: 20 additions & 5 deletions examples/helloworld/lib/server.ex
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
defmodule Helloworld.Greeter.Server do
use GRPC.Server, service: Helloworld.Greeter.Service
use GRPC.Server,
service: Helloworld.Greeter.Service,
http_transcode: true

@spec say_hello(Helloworld.HelloRequest.t(), GRPC.Server.Stream.t()) ::
Helloworld.HelloReply.t()
def say_hello(request, _stream) do
Helloworld.HelloReply.new(
message: "Hello #{request.name}",
today: today()
)
end

@spec say_hello_from(Helloworld.HelloFromRequest.t(), GRPC.Server.Stream.t()) ::
Helloworld.HelloReply.t()
def say_hello_from(request, _stream) do
Helloworld.HelloReply.new(
message: "Hello #{request.name}. From #{request.from}",
today: today()
)
end

defp today do
nanos_epoch = System.system_time() |> System.convert_time_unit(:native, :nanosecond)
seconds = div(nanos_epoch, 1_000_000_000)
nanos = nanos_epoch - seconds * 1_000_000_000

Helloworld.HelloReply.new(
message: "Hello #{request.name}",
today: %Google.Protobuf.Timestamp{seconds: seconds, nanos: nanos}
)
%Google.Protobuf.Timestamp{seconds: seconds, nanos: nanos}
end
end
1 change: 1 addition & 0 deletions examples/helloworld/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ defmodule Helloworld.Mixfile do
defp deps do
[
{:grpc, path: "../../"},
{:jason, "~> 1.3.0"},
{:protobuf, "~> 0.11"},
{:google_protos, "~> 0.3.0"},
{:dialyxir, "~> 1.1", only: [:dev, :test], runtime: false}
Expand Down
1 change: 1 addition & 0 deletions examples/helloworld/mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
"google_protos": {:hex, :google_protos, "0.3.0", "15faf44dce678ac028c289668ff56548806e313e4959a3aaf4f6e1ebe8db83f4", [:mix], [{:protobuf, "~> 0.10", [hex: :protobuf, repo: "hexpm", optional: false]}], "hexpm", "1f6b7fb20371f72f418b98e5e48dae3e022a9a6de1858d4b254ac5a5d0b4035f"},
"gun": {:hex, :grpc_gun, "2.0.1", "221b792df3a93e8fead96f697cbaf920120deacced85c6cd3329d2e67f0871f8", [:rebar3], [{:cowlib, "~> 2.11", [hex: :cowlib, repo: "hexpm", optional: false]}], "hexpm", "795a65eb9d0ba16697e6b0e1886009ce024799e43bb42753f0c59b029f592831"},
"jason": {:hex, :jason, "1.3.0", "fa6b82a934feb176263ad2df0dbd91bf633d4a46ebfdffea0c8ae82953714946", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "53fc1f51255390e0ec7e50f9cb41e751c260d065dcba2bf0d08dc51a4002c2ac"},
"protobuf": {:hex, :protobuf, "0.11.0", "58d5531abadea3f71135e97bd214da53b21adcdb5b1420aee63f4be8173ec927", [:mix], [{:jason, "~> 1.2", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "30ad9a867a5c5a0616cac9765c4d2c2b7b0030fa81ea6d0c14c2eb5affb6ac52"},
"ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"},
}
31 changes: 31 additions & 0 deletions examples/helloworld/priv/protos/google/api/annotations.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2015 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package google.api;

import "google/api/http.proto";
import "google/protobuf/descriptor.proto";

option go_package = "google.golang.org/genproto/googleapis/api/annotations;annotations";
option java_multiple_files = true;
option java_outer_classname = "AnnotationsProto";
option java_package = "com.google.api";
option objc_class_prefix = "GAPI";

extend google.protobuf.MethodOptions {
// See `HttpRule`.
HttpRule http = 72295728;
}
Loading
Loading