Skip to content

Commit 61bf997

Browse files
authored
feat(grpc/grpc-gateway): Add grpcgateway package (#212)
2 parents 8042d1f + 75e421e commit 61bf997

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

grpc/go.mod

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/kunitsucom/util.go/grpc
33
go 1.21
44

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

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

14-
require golang.org/x/sys v0.17.0 // indirect
15+
require (
16+
golang.org/x/net v0.21.0 // indirect
17+
golang.org/x/sys v0.17.0 // indirect
18+
golang.org/x/text v0.14.0 // indirect
19+
google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de // indirect
20+
)

grpc/go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
22
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
3+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 h1:/c3QmbOGMGTOumP2iT/rCwB7b0QDGLKzqOmktBjT+Is=
4+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1/go.mod h1:5SN9VR2LTsRFsrEC6FHgRbTWrTHu6tqPeKxEQv15giM=
35
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
46
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
57
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
68
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
79
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
810
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
11+
google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de h1:F6qOa9AZTYJXOUEr4jDysRDLrm4PHePlge4v4TGAlxY=
12+
google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de/go.mod h1:VUhTRKeHn9wwcdrk73nvdC9gF178Tzhmt/qyaFcPLSo=
13+
google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de h1:jFNzHPIeuzhdRwVhbZdiym9q0ory/xY3sA+v2wPg8I0=
14+
google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de/go.mod h1:5iCWqnniDlqZHrd3neWVTOwvh/v6s3232omMecelax8=
915
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de h1:cZGRis4/ot9uVm639a+rHCUaG0JJHEsdyzSQTMX+suY=
1016
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de/go.mod h1:H4O17MA/PE9BsGx3w+a+W2VOLLD1Qf7oJneAoU6WktY=
1117
google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM=

grpc/grpc-gateway/register.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package grpcgateway
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
8+
"google.golang.org/grpc"
9+
)
10+
11+
func RegisterServices(ctx context.Context, grpcServer grpc.ServiceRegistrar, mux *runtime.ServeMux, conn *grpc.ClientConn, registrars ...*GRPCServiceRegistrar) error {
12+
for _, r := range registrars {
13+
grpcServer.RegisterService(r.grpcServiceDesc, r.grpcServer)
14+
if err := r.grpcGatewayHandler(ctx, mux, conn); err != nil {
15+
return fmt.Errorf("r.grpcGatewayHandler: %s: %w", r.grpcServiceDesc.ServiceName, err)
16+
}
17+
}
18+
19+
return nil
20+
}
21+
22+
type GRPCServiceRegistrar struct {
23+
grpcServiceDesc *grpc.ServiceDesc
24+
grpcServer interface{}
25+
grpcGatewayHandler func(ctx context.Context, mux *runtime.ServeMux, conn *grpc.ClientConn) error
26+
}
27+
28+
func NewGRPCServiceRegistrar[GRPCServiceServer interface{}](
29+
grpcServiceDesc *grpc.ServiceDesc,
30+
grpcServer GRPCServiceServer,
31+
grpcGatewayHandler func(ctx context.Context, mux *runtime.ServeMux, conn *grpc.ClientConn) error,
32+
) *GRPCServiceRegistrar {
33+
return &GRPCServiceRegistrar{
34+
grpcServiceDesc: grpcServiceDesc,
35+
grpcServer: grpcServer,
36+
grpcGatewayHandler: grpcGatewayHandler,
37+
}
38+
}

0 commit comments

Comments
 (0)