namely / docker-protoc

Docker images for generating protocol buffer definitions
BSD 3-Clause "New" or "Revised" License
729 stars 224 forks source link

undefined: service.RegisterServiceHandlerFromEndpoint #378

Closed dhdevdk closed 1 year ago

dhdevdk commented 1 year ago

Hello, basically I want to set up a minimal working example and test it. I have a single file in a directory hello_world.proto, which content is copied from https://github.com/grpc-ecosystem/grpc-gateway/blob/main/examples/internal/helloworld/helloworld.proto .

Then I run

docker run -v `pwd`:/defs namely/gen-grpc-gateway -f hello_world.proto --generate-unbound-methods -s Service
docker build -t my-grpc-gateway gen/grpc-gateway/

and get this error:

(gs1-encoder-decoder-mircoservice-9p1Xk6fx-py3.9) user@dev-deltaKonnect:/projects/gs1-encoder-decoder-mircoservice/proto$ docker build -t my-grpc-gateway gen/grpc-gateway/ 
[+] Building 12.4s (13/16)                                                                                                                                                                                                                           docker:default
 => [internal] load .dockerignore                                                                                                                                                                                                                              0.0s
 => => transferring context: 2B                                                                                                                                                                                                                                0.0s
 => [internal] load build definition from Dockerfile                                                                                                                                                                                                           0.0s
 => => transferring dockerfile: 693B                                                                                                                                                                                                                           0.0s
 => [internal] load metadata for docker.io/library/alpine:3.18                                                                                                                                                                                                 0.9s
 => [internal] load metadata for docker.io/library/golang:1.19-alpine3.18                                                                                                                                                                                      0.9s
 => [build 1/6] FROM docker.io/library/golang:1.19-alpine3.18@sha256:c5e8766b6a7b77fd60a48c2b932f7ea66f7001489c4cf18d0265cff595613bee                                                                                                                          0.0s
 => [internal] load build context                                                                                                                                                                                                                              0.0s
 => => transferring context: 106.70kB                                                                                                                                                                                                                          0.0s
 => [stage-1 1/5] FROM docker.io/library/alpine:3.18@sha256:7144f7bab3d4c2648d7e59409f15ec52a18006a128c733fcff20d3a4a54ba44a                                                                                                                                   0.0s
 => CACHED [stage-1 2/5] WORKDIR /app                                                                                                                                                                                                                          0.0s
 => CACHED [build 2/6] RUN apk add --update --no-cache git                                                                                                                                                                                                     0.0s
 => CACHED [build 3/6] WORKDIR /app                                                                                                                                                                                                                            0.0s
 => [build 4/6] COPY . /app/                                                                                                                                                                                                                                   0.1s
 => [build 5/6] RUN go mod tidy -compat=1.19                                                                                                                                                                                                                   7.0s
 => ERROR [build 6/6] RUN go build -o /app/grpc_gateway cmd/gateway/main.go                                                                                                                                                                                    4.3s
------                                                                                                                                                                                                                                                              
 > [build 6/6] RUN go build -o /app/grpc_gateway cmd/gateway/main.go:                                                                                                                                                                                               
3.991 # command-line-arguments                                                                                                                                                                                                                                      
3.991 cmd/gateway/main.go:347:12: undefined: service.RegisterServiceHandlerFromEndpoint                                                                                                                                                                             
------                                                                                                                                                                                                                                                              
Dockerfile:18
--------------------
  16 |     
  17 |     # Build the gateway
  18 | >>> RUN go build -o /app/grpc_gateway cmd/gateway/main.go
  19 |     
  20 |     FROM alpine:$alpine_version
--------------------
ERROR: failed to solve: process "/bin/sh -c go build -o /app/grpc_gateway cmd/gateway/main.go" did not complete successfully: exit code: 1

I am not sure if I am doing something wrong or if this is a real bug. From the docs it says, that one should provide at least one http binding or add the option --generate-unbound-methods. I have done both and still get this error.

I would really appreciate any help to get this working. Thanks in advance.

Edit:

I had to change the line

option go_package =  github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/helloworld

to something else like

option go_package = github.com/yourorg/yourprotos/gen/go/your/service

because otherwise I get this error

 => ERROR [build 6/6] RUN go build -o /app/grpc_gateway cmd/gateway/main.go                                                                                                                                                                                    0.9s
------                                                                                                                                                                                                                                                              
 > [build 6/6] RUN go build -o /app/grpc_gateway cmd/gateway/main.go:                                                                                                                                                                                               
0.833 package command-line-arguments                                                                                                                                                                                                                                
0.833   cmd/gateway/main.go:27:2: use of internal package gateway/gen/github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/helloworld not allowed                                                                                                            
------                                                                                                                                                                                                                                                              
Dockerfile:18
--------------------
  16 |     
  17 |     # Build the gateway
  18 | >>> RUN go build -o /app/grpc_gateway cmd/gateway/main.go
  19 |     
  20 |     FROM alpine:$alpine_version
--------------------
ERROR: failed to solve: process "/bin/sh -c go build -o /app/grpc_gateway cmd/gateway/main.go" did not complete successfully: exit code: 1
abe545 commented 1 year ago

@dhdevdk does that mean you were able to get your sample building and working?

dhdevdk commented 1 year ago

@abe545 No, I was able to generate the code via

docker run -v `pwd`:/defs namely/gen-grpc-gateway -f hello_world.proto --generate-unbound-methods -s Service

Then I have the output:

/tmp/tmp$ tree gen/
gen/
└── grpc-gateway
    ├── cmd
    │   └── gateway
    │       └── main.go
    ├── config.yaml
    ├── Dockerfile
    ├── gen
    │   ├── github.com
    │   │   └── yourorg
    │   │       └── yourprotos
    │   │           └── gen
    │   │               └── go
    │   │                   └── your
    │   │                       └── service
    │   │                           ├── helloworld_grpc.pb.go
    │   │                           ├── helloworld.pb.go
    │   │                           └── helloworld.pb.gw.go
    │   └── helloworld.swagger.json
    └── go.mod

Here is the related code so you are able to reproduce the error (https://gist.github.com/dhdevdk/32c94651d7f590a8ff94d537a4e44555) (I had to replace "/" with "--" in the paths.)

Then this command

docker build -t my-grpc-gateway gen/grpc-gateway/

fails with the reported error.

abe545 commented 1 year ago

Ah, ok @dhdevdk. So I'm not a golang dev myself, but as far as that error with internal, I believe that is a golang convention. If you just delete that word from the package path, you'll be able to bypass that error. I pulled down the example protofile, and put it in a directory named greeter because that is how we structure our protos. Then from the directory that contains the greeter folder, this command generates the gateway from the proto unmodified (I used Greeter as the service name because that is the name of the service in the proto):

docker run -v `pwd`:/defs namely/gen-grpc-gateway -f greeter/greeter.proto -s Greeter

When I tried to build the generated code, it gave the golang internal package error you described. I then removed internal from the proto's go_package option, and then ran into an error from docker with the swagger file being in the wrong spot. I think this might actually be a bug in this generator. I'll look into that. But I was able to correct the swagger file location in the dockerfile manually, and that was the only other change I had to make to get this to build.

dhdevdk commented 1 year ago

@abe545 Ah ok, I missed that the -s option should match the service name. I was also getting an error with the json file and fixed the paths.

So it's working now. Thanks!

One more question: I can access the swagger json at /swagger.json, but is there a way to generate an openapi 3.0 compatible json/yaml file?

abe545 commented 1 year ago

@dhdevdk - glad you got it working. This is a thin wrapper around https://github.com/grpc-ecosystem/grpc-gateway, so if they have an option to do so, it might just work to pass it in. If they don't have that option, there is nothing we can do. If they do have an option, and there is no way to pass it down, we'd be happy to accept a PR that adds support for it.

dhdevdk commented 1 year ago

@abe545 Okay, I will have a look at it. I need openapi 3.0, since I am using enums, which are not supported in 2.0. I think I will try to generate an openapi 3.0 spec via https://github.com/google/gnostic/tree/main/cmd/protoc-gen-openapi and try out whether it is working properly.

So shall I close this issue?

abe545 commented 1 year ago

@dhdevdk yeah, I'll close this as it seems like you are able to codegen & build it.