Protoc plugin to use composition on gRPC server.
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 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:
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:
translator := translate.NewGoogleTranslator()
wrapper := NewTranslatorEndpointsWrapper(TranslatorEndpoints{
TranslateEndpoint: NewTranslateEndpoint(translator),
})
s := grpc.NewServer()
proto.RegisterTranslatorServer(s, wrapper)
s.Serve(lis)
ServiceX represents application services like database repository or domain service.
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()
}
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)
}