2019-06-08 19:40:44 +01:00
|
|
|
// Package mucp initialises a mucp service
|
|
|
|
package mucp
|
|
|
|
|
|
|
|
import (
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/client"
|
|
|
|
cmucp "github.com/micro/go-micro/v2/client/mucp"
|
2020-07-11 21:15:59 +01:00
|
|
|
"github.com/micro/go-micro/v2/model"
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/server"
|
|
|
|
smucp "github.com/micro/go-micro/v2/server/mucp"
|
|
|
|
"github.com/micro/go-micro/v2/service"
|
2019-06-08 19:40:44 +01:00
|
|
|
)
|
|
|
|
|
2019-12-29 21:07:55 +00:00
|
|
|
type mucpService struct {
|
|
|
|
opts service.Options
|
|
|
|
}
|
|
|
|
|
|
|
|
func newService(opts ...service.Option) service.Service {
|
|
|
|
options := service.NewOptions(opts...)
|
|
|
|
|
|
|
|
return &mucpService{
|
|
|
|
opts: options,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *mucpService) Name() string {
|
|
|
|
return s.opts.Server.Options().Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init initialises options. Additionally it calls cmd.Init
|
|
|
|
// which parses command line flags. cmd.Init is only called
|
|
|
|
// on first Init.
|
|
|
|
func (s *mucpService) Init(opts ...service.Option) {
|
|
|
|
// process options
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&s.opts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *mucpService) Options() service.Options {
|
|
|
|
return s.opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *mucpService) Client() client.Client {
|
|
|
|
return s.opts.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *mucpService) Server() server.Server {
|
|
|
|
return s.opts.Server
|
|
|
|
}
|
|
|
|
|
2020-07-11 21:15:59 +01:00
|
|
|
func (s *mucpService) Model() model.Model {
|
|
|
|
return s.opts.Model
|
|
|
|
}
|
|
|
|
|
2019-12-29 21:07:55 +00:00
|
|
|
func (s *mucpService) String() string {
|
|
|
|
return "mucp"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *mucpService) 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 *mucpService) 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 *mucpService) Run() error {
|
|
|
|
if err := s.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait on context cancel
|
|
|
|
<-s.opts.Context.Done()
|
|
|
|
|
|
|
|
return s.Stop()
|
|
|
|
}
|
|
|
|
|
2019-06-08 19:40:44 +01:00
|
|
|
// NewService returns a new mucp service
|
2019-12-29 21:07:55 +00:00
|
|
|
func NewService(opts ...service.Option) service.Service {
|
|
|
|
options := []service.Option{
|
|
|
|
service.Client(cmucp.NewClient()),
|
|
|
|
service.Server(smucp.NewServer()),
|
2019-06-08 19:40:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
options = append(options, opts...)
|
|
|
|
|
2019-12-29 21:07:55 +00:00
|
|
|
return newService(options...)
|
2019-06-08 19:40:44 +01:00
|
|
|
}
|