https://github.com/qneyrat/go-grpc-endpoints
protoc plugin to auto-generate endpoints layer
https://github.com/qneyrat/go-grpc-endpoints
grpc protobuf protoc
Last synced: about 1 year ago
JSON representation
protoc plugin to auto-generate endpoints layer
- Host: GitHub
- URL: https://github.com/qneyrat/go-grpc-endpoints
- Owner: qneyrat
- Created: 2018-08-21T16:57:28.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-02-11T15:39:33.000Z (over 7 years ago)
- Last Synced: 2025-03-31T07:01:33.992Z (about 1 year ago)
- Topics: grpc, protobuf, protoc
- Language: Go
- Homepage:
- Size: 170 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# go-grpc-endpoints
Protoc plugin to use composition on gRPC server.
## Installing and using
Install with `go get`:
```
> $ go get github.com/qneyrat/go-grpc-endpoints/protoc-gen-goendpoints
> $ go install github.com/qneyrat/go-grpc-endpoints/protoc-gen-goendpoints
```
Add `--goendpoints_out=` flag to generage endpoints layer with protoc:
```
> $ protoc -I . ./translator.proto --go_out=plugins=grpc:. --goendpoints_out=.:.
```
## Plugin and endpoints layer usage
Plugin auto-generate strucs and func for endpoints layer from your proto files.
EndpointsWrapper is gRPC server. It's implement gRPC server interface and call endpoints
Endpoints struct is endpoints group to create EndpointsWrapper.
You can just write EndpointFunc constructor to hydrate Endpoints struct:
- create EndpointFunc constructor:
```go
func NewTranslateEndpoint(t translate.Translator) TranslateEndpointFunc {
return func(ctx context.Context, req *proto.TranslateRequest) (*proto.TranslateResponse, error) {
// use t.Translate here
}
}
```
- register EndpointsWrapper on gRPC server:
```go
translator := translate.NewGoogleTranslator()
wrapper := NewTranslatorEndpointsWrapper(TranslatorEndpoints{
TranslateEndpoint: NewTranslateEndpoint(translator),
})
s := grpc.NewServer()
proto.RegisterTranslatorServer(s, wrapper)
s.Serve(lis)
```
## Theory
ServiceX represents application services like database repository or domain service.
### gRPC Server without composition
```Go
type Server struct{
service1 Service1
service2 Service2
service3 Service3
// ...
}
func (s *Server) Method(ctx context.Context, req *proto.Req) (*proto.Res, error) {
// use services here like s.service1.Foo()
}
```
### gRPC Server with composition and introduce endpoints layer
```Go
type EndpointFunc1 func(ctx context.Context, req *proto.Req) (*proto.Res, error)
func NewEndpointFunc1(service1 Service1, service2 Service2) EndpointFunc1 {
return func(ctx context.Context, req *proto.Req) (*proto.Res, error) {
// use services here like s.service1.Foo()
}
}
type Server struct{
endpoint1 EndpointFunc1
// ...
}
func (s *Server) Method(ctx context.Context, req *proto.Req) (*proto.Res, error) {
return s.endpoint1(ctx, req)
}
```