Skip to content

Commit 13c6275

Browse files
authored
Grpc transcode (#3)
* transcode proto build * Generate Register MUX handler * Working gRPC and Transcode
1 parent c1a0b31 commit 13c6275

File tree

18 files changed

+693
-100
lines changed

18 files changed

+693
-100
lines changed

.vscode/launch.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
"LOG_LEVEL": "debug",
3333
"PRETTY_LOG":"true",
3434
"PORT": "50051",
35+
"GRPC_GATEWAY_ENABLED": "true",
36+
"REST_PORT": "50052",
3537
"CUSTOM_STRING": "In Flames",
3638
"SOME_SECRET": "1234567890",
3739
"ENABLE_GRPC_SERVER_REFLECTION": "true",
@@ -50,6 +52,9 @@
5052
"LOG_LEVEL": "debug",
5153
"PRETTY_LOG":"true",
5254
"PORT": "50051",
55+
"GRPC_GATEWAY_ENABLED": "true",
56+
"REST_PORT": "50052",
57+
"GRPCPort": "50051",
5358
"CUSTOM_STRING": "In Flames",
5459
"SOME_SECRET": "1234567890",
5560
},

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
## Build the proto
44

55
[grpc.io](https://grpc.io/docs/languages/go/basics/)
6+
[Transcode Ref](https://grpc-ecosystem.github.io/grpc-gateway/docs/tutorials/introduction/)
7+
[custom protoc plugin](https://rotemtam.com/2021/03/22/creating-a-protoc-plugin-to-gen-go-code/)
8+
9+
```bash
10+
go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@latest
11+
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
12+
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
13+
```
614

715
```powershell
816
go mod tidy
@@ -13,13 +21,25 @@ protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=p
1321
1422
```
1523

24+
### grpc Only
25+
1626
```powershell
1727
go mod tidy
1828
go build .\protoc-gen-go-fluffycore-di\cmd\protoc-gen-go-fluffycore-di\
1929
protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative --go-fluffycore-di_out=. --go-fluffycore-di_opt=paths=source_relative ./proto/helloworld/helloworld.proto
2030
2131
```
2232

33+
### grpc and gateway
34+
35+
```powershell
36+
go mod tidy
37+
go build .\protoc-gen-go-fluffycore-di\cmd\protoc-gen-go-fluffycore-di\
38+
39+
protoc --go_out=. --go_opt paths=source_relative --grpc-gateway_out . --grpc-gateway_opt paths=source_relative --go-grpc_out . --go-grpc_opt paths=source_relative --go-fluffycore-di_out . --go-fluffycore-di_opt paths=source_relative,grpc_gateway=true ./proto/helloworld/helloworld.proto
40+
41+
```
42+
2343
## Why the custome protoc plugin?
2444

2545
The main reason is that we want a middleware to create a scoped container. We want each request to instantiate the handler object within that container and subsequently only live for the life of the request. Just like in the asp.net core world. Since the proto defines the service we generate a shim for each call. Imagine if the service had hundres of methods. We need that code generated.

contracts/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ type (
55
ApplicationEnvironment string `json:"applicationEnvironment" mapstructure:"APPLICATION_ENVIRONMENT"`
66
ApplicationName string `json:"applicationName" mapstructure:"APPLICATION_NAME"`
77
PORT int `json:"port" mapstructure:"PORT"`
8+
GRPCGateWayEnabled bool `json:"grpcGateWayEnabled" mapstructure:"GRPC_GATEWAY_ENABLED"`
9+
RESTPort int `json:"restPort" mapstructure:"REST_PORT"`
810
PrettyLog bool `json:"prettyLog" mapstructure:"PRETTY_LOG"`
911
LogLevel string `json:"logLevel" mapstructure:"LOG_LEVEL"`
1012
}

contracts/endpoint/endpoint.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package endpoint
22

3-
import "google.golang.org/grpc"
3+
import (
4+
grpc_gateway_runtime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
5+
"google.golang.org/grpc"
6+
)
47

58
type (
69
IEndpointRegistration interface {
710
Register(s *grpc.Server)
11+
RegisterHandler(gwmux *grpc_gateway_runtime.ServeMux, conn *grpc.ClientConn)
812
}
913
)

example/internal/contracts/config/config.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ var ConfigDefaultJSON = []byte(`
1919
"LOG_LEVEL": "info",
2020
"PORT": 1111,
2121
"CUSTOM_STRING": "some default value",
22-
"SOME_SECRET": "password"
22+
"SOME_SECRET": "password",
23+
"REST_PORT": 50052,
24+
"GRPC_GATEWAY_ENABLED": true
25+
2326
}
2427
`)

example/internal/services/greeter/greeter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import (
77
contracts_config "github.com/fluffy-bunny/fluffycore/example/internal/contracts/config"
88
fluffycore_contracts_somedisposable "github.com/fluffy-bunny/fluffycore/example/internal/contracts/somedisposable"
99
proto_helloworld "github.com/fluffy-bunny/fluffycore/proto/helloworld"
10-
"github.com/gogo/status"
1110
"github.com/rs/zerolog"
12-
"google.golang.org/grpc/codes"
1311
)
1412

1513
type (
@@ -23,7 +21,9 @@ type (
2321
func (s *service) SayHello(ctx context.Context, request *proto_helloworld.HelloRequest) (*proto_helloworld.HelloReply, error) {
2422
log := zerolog.Ctx(ctx)
2523
log.Info().Msg("SayHello")
26-
return nil, status.Errorf(codes.Unimplemented, "method SayHello not implemented for %s", s.config.CoreConfig.ApplicationName)
24+
return &proto_helloworld.HelloReply{
25+
Message: "Hello " + request.Name,
26+
}, nil
2727
}
2828
func AddGreeterService(builder di.ContainerBuilder) {
2929
proto_helloworld.AddGreeterServer[proto_helloworld.IGreeterServer](builder,

go.mod

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ replace github.com/dozm/di => github.com/fluffy-bunny/dozm-di v0.3.3
66

77
require (
88
github.com/auth0/go-jwt-middleware/v2 v2.1.0
9+
github.com/common-nighthawk/go-figure v0.0.0-20210622060536-734e95fb86be
910
github.com/dozm/di v0.3.0
1011
github.com/fatih/structs v1.1.0
1112
github.com/fluffy-bunny/sarulabsdi v0.1.65
@@ -15,6 +16,7 @@ require (
1516
github.com/golang-jwt/jwt v3.2.2+incompatible
1617
github.com/golang/mock v1.6.0
1718
github.com/google/uuid v1.3.0
19+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0
1820
github.com/jedib0t/go-pretty/v6 v6.4.6
1921
github.com/jellydator/ttlcache/v2 v2.11.1
2022
github.com/jinzhu/copier v0.3.5
@@ -34,11 +36,15 @@ require (
3436
github.com/tkuchiki/parsetime v0.3.0
3537
github.com/tufin/asciitree v0.0.0-20210127111056-bf70173ef677
3638
github.com/ziflex/lecho v1.2.0
39+
google.golang.org/api v0.114.0
40+
google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc
3741
google.golang.org/grpc v1.55.0
3842
google.golang.org/protobuf v1.30.0
3943
)
4044

4145
require (
46+
cloud.google.com/go/compute v1.19.0 // indirect
47+
cloud.google.com/go/compute/metadata v0.2.3 // indirect
4248
github.com/KyleBanks/depth v1.2.1 // indirect
4349
github.com/PuerkitoBio/purell v1.1.1 // indirect
4450
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
@@ -55,12 +61,14 @@ require (
5561
github.com/goccy/go-json v0.10.2 // indirect
5662
github.com/gogo/googleapis v1.4.1 // indirect
5763
github.com/gogo/protobuf v1.3.2 // indirect
64+
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
5865
github.com/golang/protobuf v1.5.3 // indirect
5966
github.com/google/pprof v0.0.0-20230323073829-e72429f035bd // indirect
67+
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
68+
github.com/googleapis/gax-go/v2 v2.7.1 // indirect
6069
github.com/hashicorp/hcl v1.0.0 // indirect
6170
github.com/inconshreveable/mousetrap v1.1.0 // indirect
6271
github.com/josharian/intern v1.0.0 // indirect
63-
github.com/kr/pretty v0.3.1 // indirect
6472
github.com/labstack/echo v3.3.10+incompatible // indirect
6573
github.com/lestrrat-go/blackmagic v1.0.1 // indirect
6674
github.com/lestrrat-go/httpcc v1.0.1 // indirect
@@ -77,6 +85,7 @@ require (
7785
github.com/pkg/errors v0.9.1 // indirect
7886
github.com/pmezard/go-difflib v1.0.0 // indirect
7987
github.com/rivo/uniseg v0.4.4 // indirect
88+
github.com/rogpeppe/go-internal v1.9.0 // indirect
8089
github.com/spf13/afero v1.9.5 // indirect
8190
github.com/spf13/cast v1.5.0 // indirect
8291
github.com/spf13/jwalterweatherman v1.1.0 // indirect
@@ -86,14 +95,18 @@ require (
8695
github.com/tkuchiki/go-timezone v0.2.2 // indirect
8796
github.com/valyala/bytebufferpool v1.0.0 // indirect
8897
github.com/valyala/fasttemplate v1.2.2 // indirect
98+
go.opencensus.io v0.24.0 // indirect
8999
golang.org/x/crypto v0.7.0 // indirect
90-
golang.org/x/net v0.8.0 // indirect
100+
golang.org/x/net v0.10.0 // indirect
101+
golang.org/x/oauth2 v0.8.0 // indirect
91102
golang.org/x/sync v0.1.0 // indirect
92-
golang.org/x/sys v0.6.0 // indirect
93-
golang.org/x/text v0.8.0 // indirect
103+
golang.org/x/sys v0.8.0 // indirect
104+
golang.org/x/text v0.9.0 // indirect
94105
golang.org/x/time v0.3.0 // indirect
95106
golang.org/x/tools v0.7.0 // indirect
96-
google.golang.org/genproto v0.0.0-20230323212658-478b75c54725 // indirect
107+
google.golang.org/appengine v1.6.7 // indirect
108+
google.golang.org/genproto v0.0.0-20230526203410-71b5a4ffd15e // indirect
109+
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
97110
gopkg.in/ini.v1 v1.67.0 // indirect
98111
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
99112
gopkg.in/yaml.v2 v2.4.0 // indirect

0 commit comments

Comments
 (0)