Closed drowzy closed 5 months ago
@drowzy I prefer the first approach to generate service.pb.svc.ex files.
Extensions cannot be provided at runtime (at least to my knowledge) only those defined in elixir-protobuf already.
This seems like an upstream issue that could be tackled in :protobuf
. Perhaps they could provide a config (either application config or a config file like Credo uses) for that?
This seems like an upstream issue that could be tackled in
:protobuf
. Perhaps they could provide a config (either application config or a config file like Credo uses) for that?
Hmm, yes the issue more or less resolves itself if the code gen was called per project instead through a global script via protoc. I'll create an issue in protobuf and we'll see :)
From a user perspective it involves adding gRPC-gateway annotations to your proto to define how JSON requests and responses should be mapped to the gRPC service. The benefit is that you can offer a REST api in addition to gRPC, but you're only required to implement the handler once.
For instance the
helloworld.proto
would look something like this:SayHello
-curl -XGET localhost:50051/v1/greeter/test
SayHelloFrom
-curl -XPOST -H 'Content-type: application/json' -d '{"name": "test", "from": "anon"}' http://localhost:50051/v1/greeter
There's a draft PR to check out: #273 Related issue: #192
Caveats / shortcomings
protoc
plugin from protobuf callsProtobuf.load_extensions/0
which loads any protobuf extensions available in the loaded applications. In order for theMethodDescriptor
to include theGoogle.Api.HttpRule
these need to be already generated so thatProtobuf.load_extensions/0
picks them up when runningprotoc
. As the grpc generator is called from the elixir-protobuf escript plugin. Extensions cannot be provided at runtime (at least to my knowledge) only those defined in elixir-protobuf already.Two possible solutions:
protoc
plugin in this library which supersedes the grpc plugin option in protobuf generating the protobuf message definitions and grpc service definition would require a call sort of like this:Which would create two files in
lib
. One with the messages (helloworld.pb.ex
) and one with the service definitionshelloworld.pb.svc.ex
). This is the approach used in twirp for instance.protoc
plugin in a different repository (likegrpc-gateway
) which would include theGoogle.Api
extensions instead.