2019-06-03 20:44:43 +03:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
2019-12-30 00:07:55 +03:00
|
|
|
"github.com/micro/go-micro/client"
|
|
|
|
gclient "github.com/micro/go-micro/client/grpc"
|
|
|
|
"github.com/micro/go-micro/server"
|
|
|
|
gserver "github.com/micro/go-micro/server/grpc"
|
|
|
|
"github.com/micro/go-micro/service"
|
2019-06-03 20:44:43 +03:00
|
|
|
)
|
|
|
|
|
2019-12-30 00:07:55 +03:00
|
|
|
type grpcService struct {
|
|
|
|
opts service.Options
|
|
|
|
}
|
2019-06-03 20:44:43 +03:00
|
|
|
|
2019-12-30 00:07:55 +03:00
|
|
|
func newService(opts ...service.Option) service.Service {
|
|
|
|
options := service.NewOptions(opts...)
|
|
|
|
|
|
|
|
return &grpcService{
|
|
|
|
opts: options,
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
2019-12-30 00:07:55 +03:00
|
|
|
}
|
2019-06-03 20:44:43 +03:00
|
|
|
|
2019-12-30 00:07:55 +03:00
|
|
|
func (s *grpcService) Name() string {
|
|
|
|
return s.opts.Server.Options().Name
|
|
|
|
}
|
2019-06-03 20:44:43 +03:00
|
|
|
|
2019-12-30 00:07:55 +03:00
|
|
|
// Init initialises options. Additionally it calls cmd.Init
|
|
|
|
// which parses command line flags. cmd.Init is only called
|
|
|
|
// on first Init.
|
|
|
|
func (s *grpcService) Init(opts ...service.Option) {
|
|
|
|
// process options
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&s.opts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *grpcService) Options() service.Options {
|
|
|
|
return s.opts
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
|
|
|
|
2019-12-30 00:07:55 +03:00
|
|
|
func (s *grpcService) Client() client.Client {
|
|
|
|
return s.opts.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *grpcService) Server() server.Server {
|
|
|
|
return s.opts.Server
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *grpcService) String() string {
|
|
|
|
return "grpc"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *grpcService) Start() error {
|
|
|
|
for _, fn := range s.opts.BeforeStart {
|
|
|
|
if err := fn(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.opts.Server.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fn := range s.opts.AfterStart {
|
|
|
|
if err := fn(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *grpcService) Stop() error {
|
|
|
|
var gerr error
|
|
|
|
|
|
|
|
for _, fn := range s.opts.BeforeStop {
|
|
|
|
if err := fn(); err != nil {
|
|
|
|
gerr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.opts.Server.Stop(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fn := range s.opts.AfterStop {
|
|
|
|
if err := fn(); err != nil {
|
|
|
|
gerr = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return gerr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *grpcService) Run() error {
|
|
|
|
if err := s.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait on context cancel
|
|
|
|
<-s.opts.Context.Done()
|
|
|
|
|
|
|
|
return s.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewService returns a grpc service compatible with go-micro.Service
|
|
|
|
func NewService(opts ...service.Option) service.Service {
|
2019-06-03 20:44:43 +03:00
|
|
|
// our grpc client
|
2019-12-30 00:07:55 +03:00
|
|
|
c := gclient.NewClient()
|
2019-06-03 20:44:43 +03:00
|
|
|
// our grpc server
|
2019-12-30 00:07:55 +03:00
|
|
|
s := gserver.NewServer()
|
2019-06-03 20:44:43 +03:00
|
|
|
|
|
|
|
// create options with priority for our opts
|
2019-12-30 00:07:55 +03:00
|
|
|
options := []service.Option{
|
|
|
|
service.Client(c),
|
|
|
|
service.Server(s),
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// append passed in opts
|
|
|
|
options = append(options, opts...)
|
|
|
|
|
2019-12-30 00:07:55 +03:00
|
|
|
// generate and return a service
|
|
|
|
return newService(options...)
|
2019-06-03 20:44:43 +03:00
|
|
|
}
|