Skip to content

Commit

Permalink
feat(grpc/grpc-gateway): Add grpcgateway package
Browse files Browse the repository at this point in the history
  • Loading branch information
ginokent committed May 7, 2024
1 parent 8042d1f commit 75e421e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
8 changes: 7 additions & 1 deletion grpc/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/kunitsucom/util.go/grpc
go 1.21

require (
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1
github.com/kunitsucom/util.go v0.0.0-00010101000000-000000000000
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de
google.golang.org/grpc v1.63.2
Expand All @@ -11,4 +12,9 @@ require (

replace github.com/kunitsucom/util.go => ../../util.go

require golang.org/x/sys v0.17.0 // indirect
require (
golang.org/x/net v0.21.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de // indirect
)
6 changes: 6 additions & 0 deletions grpc/go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 h1:/c3QmbOGMGTOumP2iT/rCwB7b0QDGLKzqOmktBjT+Is=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1/go.mod h1:5SN9VR2LTsRFsrEC6FHgRbTWrTHu6tqPeKxEQv15giM=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de h1:F6qOa9AZTYJXOUEr4jDysRDLrm4PHePlge4v4TGAlxY=
google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de/go.mod h1:VUhTRKeHn9wwcdrk73nvdC9gF178Tzhmt/qyaFcPLSo=
google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de h1:jFNzHPIeuzhdRwVhbZdiym9q0ory/xY3sA+v2wPg8I0=
google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de/go.mod h1:5iCWqnniDlqZHrd3neWVTOwvh/v6s3232omMecelax8=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de h1:cZGRis4/ot9uVm639a+rHCUaG0JJHEsdyzSQTMX+suY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de/go.mod h1:H4O17MA/PE9BsGx3w+a+W2VOLLD1Qf7oJneAoU6WktY=
google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM=
Expand Down
38 changes: 38 additions & 0 deletions grpc/grpc-gateway/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package grpcgateway

import (
"context"
"fmt"

"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
)

func RegisterServices(ctx context.Context, grpcServer grpc.ServiceRegistrar, mux *runtime.ServeMux, conn *grpc.ClientConn, registrars ...*GRPCServiceRegistrar) error {
for _, r := range registrars {
grpcServer.RegisterService(r.grpcServiceDesc, r.grpcServer)
if err := r.grpcGatewayHandler(ctx, mux, conn); err != nil {
return fmt.Errorf("r.grpcGatewayHandler: %s: %w", r.grpcServiceDesc.ServiceName, err)
}
}

return nil
}

type GRPCServiceRegistrar struct {
grpcServiceDesc *grpc.ServiceDesc
grpcServer interface{}
grpcGatewayHandler func(ctx context.Context, mux *runtime.ServeMux, conn *grpc.ClientConn) error
}

func NewGRPCServiceRegistrar[GRPCServiceServer interface{}](
grpcServiceDesc *grpc.ServiceDesc,
grpcServer GRPCServiceServer,
grpcGatewayHandler func(ctx context.Context, mux *runtime.ServeMux, conn *grpc.ClientConn) error,
) *GRPCServiceRegistrar {
return &GRPCServiceRegistrar{
grpcServiceDesc: grpcServiceDesc,
grpcServer: grpcServer,
grpcGatewayHandler: grpcGatewayHandler,
}
}

0 comments on commit 75e421e

Please sign in to comment.