-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat/http transcoding (Continuation) (#351)
* minimal working example * return correct content-type when codec is JSON * support default rpc options arg * add custom protoc plugin * include api extensions * include http options in helloworld example * dont pack message if the method was called as a transcode req * wip! build route from HttpRule * update helloworld example with HttpRule options * rename mod to transcode * fix! match rpc options in stub * encode segments to path * expose routes from server Routes are fetched from server and compiled into a dispatch conf. Instead of finding a matching server in the handler the path is matched and the handler is called with the correct server. * init http/json transcode integration tests * merge path bindings with body * http rule template lexer / parse rename literal -> identifier * fix warnings * fix! merge request params before decoding into struct * update example curl requests * Fix typo in filenames * minimal working example * return correct content-type when codec is JSON * support default rpc options arg * add custom protoc plugin * include api extensions * include http options in helloworld example * dont pack message if the method was called as a transcode req * wip! build route from HttpRule * update helloworld example with HttpRule options * rename mod to transcode * fix! match rpc options in stub * encode segments to path * expose routes from server Routes are fetched from server and compiled into a dispatch conf. Instead of finding a matching server in the handler the path is matched and the handler is called with the correct server. * init http/json transcode integration tests * merge path bindings with body * http rule template lexer / parse rename literal -> identifier * fix warnings * fix! merge request params before decoding into struct * update example curl requests * include http method in __call_rpc__ This enables to differentiate to transcoded calls which has the same URL path but different request methods * add query decoder * include query string in request mapping * map request using HttpRule.body * comply to request mapping rules by checking HttpRule.body param * support newline delimeted json for transcoded server streams * base generator on protobuf 0.11.0 * map transcoded response using HttpRule.response_body * support nested field paths in http paths * Fix! parse sub-paths for variable assignment * support calling grpc method with json if http_transcode == true * remove params from template parsing * add GRPC.Server transcode example * get requests can use accept header to find codec * remove reference to HttpRule type * rm protoc plugin in favour of protobuf_generate * add new cowboy router middleware The default routing in cowboy is not capable enough to express 'complex' routes on the form `/v1/{a=shelves/*}/books/**`. The added router is derived from `:cowboy_router` but implements new matching to allow to match on the above statement. * move routing specific func. to GRPC.Server.Router * don't generate protos before tests * reset helloworld example * add helloworld_transcode example * include google api protos in tests only * map gRPC code -> http status * Minor adjusts * Adjusts * Fix. Assert payload * Remove deprecated and fix some tests * Fix. status handler test * Update lib/grpc/codec/json.ex * Update lib/grpc/codec/json.ex * Update lib/grpc/server.ex * Update lib/grpc/transport/http2.ex * Update lib/grpc/server/transcode.ex * chore: remove prometheus stray deps --------- Co-authored-by: Simon Thörnqvist <[email protected]> Co-authored-by: Paulo Valente <[email protected]>
- Loading branch information
1 parent
ab18c93
commit 8aaf3d3
Showing
80 changed files
with
5,925 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
examples/helloworld/priv/protos/google/api/annotations.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.