0
votes

I'm trying to use https://github.com/grpc-ecosystem/grpc-gateway to enable my go grpc methods to be available to be called by http as well. To that end I'm using the module https://github.com/grpc-ecosystem/grpc-gateway. However when I generate the proto files using protoc I dont see a method Register*FromEndpoint as shown in the example. This is what my .proto file looks like

syntax = "proto3";

package health;

option go_package = "github.com/user/app/api/health";
import "google/api/annotations.proto";

service Health {
    rpc Ping (HealthRequest) returns (HealthReply) {
      option (google.api.http) = {
        get: "/ping"
      };
    }
  }
  
  // The request message containing the user's name
  message HealthRequest {    
  }
  
  // The response message containing the greetings
  message HealthReply {
    string message = 1;
  }

This is what my protoc command looks like

protoc --go_out=api/proto/ --go_opt=paths=source_relative \
    --go-grpc_out=./api/proto --go-grpc_opt=paths=source_relative \
    --proto_path=internal/api \
    --proto_path=third_party \
    ./internal/api/health/health.proto      

The generation works fine without any errors, but the generated health_grpc.pb.go file does not have the equivalent RegisterYourServiceHandlerFromEndpoint method as shown in the example here https://github.com/grpc-ecosystem/grpc-gateway

2
Your protoc options don't appear to include the flags to trigger gRPC-Gateway code generation e.g. --grpc-gateway_opt logtostderr=true --grpc-gateway_opt paths=source_relative (this is covered at the top of step 4 in the usage instructions).Brits

2 Answers

0
votes

As document of grpc-gateway saied, I suggested you to use buf rather than protoc, and it more easy and friendly.

You can reference the document of grpc-gateway#usage

version: v1beta1
plugins:
  - name: go
    out: gen/go
    opt:
      - paths=source_relative
  - name: go-grpc
    out: gen/go
    opt:
      - paths=source_relative
  - name: grpc-gateway
    out: gen/go
    opt:
      - paths=source_relative
      - generate_unbound_methods=true

And if you still want use protoc, you need add --grpc-gateway_opt param:

protoc -I . --grpc-gateway_out ./gen/go \
    --grpc-gateway_opt logtostderr=true \
    --grpc-gateway_opt paths=source_relative \
    --grpc-gateway_opt generate_unbound_methods=true \
    your/service/v1/your_service.proto
0
votes

For generating the stubs, we can use either protoc or buf. protoc is the more classic generation experience used widely in the industry. Still, it has a pretty steep learning curve. buf is a newer tool built with user experience and speed in mind. It also offers linting and breaking change detection, and something protoc doesn’t provide.

You can read more about buf here: https://grpc-ecosystem.github.io/grpc-gateway/docs/tutorials/generating_stubs/using_buf/.