github.com/annwntech/go-micro/v2@v2.9.5/service/mucp/mucp.go (about)

     1  // Package mucp initialises a mucp service
     2  package mucp
     3  
     4  import (
     5  	"github.com/annwntech/go-micro/v2/client"
     6  	cmucp "github.com/annwntech/go-micro/v2/client/mucp"
     7  	"github.com/annwntech/go-micro/v2/server"
     8  	smucp "github.com/annwntech/go-micro/v2/server/mucp"
     9  	"github.com/annwntech/go-micro/v2/service"
    10  )
    11  
    12  type mucpService struct {
    13  	opts service.Options
    14  }
    15  
    16  func newService(opts ...service.Option) service.Service {
    17  	options := service.NewOptions(opts...)
    18  
    19  	return &mucpService{
    20  		opts: options,
    21  	}
    22  }
    23  
    24  func (s *mucpService) Name() string {
    25  	return s.opts.Server.Options().Name
    26  }
    27  
    28  // Init initialises options. Additionally it calls cmd.Init
    29  // which parses command line flags. cmd.Init is only called
    30  // on first Init.
    31  func (s *mucpService) Init(opts ...service.Option) {
    32  	// process options
    33  	for _, o := range opts {
    34  		o(&s.opts)
    35  	}
    36  }
    37  
    38  func (s *mucpService) Options() service.Options {
    39  	return s.opts
    40  }
    41  
    42  func (s *mucpService) Client() client.Client {
    43  	return s.opts.Client
    44  }
    45  
    46  func (s *mucpService) Server() server.Server {
    47  	return s.opts.Server
    48  }
    49  
    50  func (s *mucpService) String() string {
    51  	return "mucp"
    52  }
    53  
    54  func (s *mucpService) Start() error {
    55  	for _, fn := range s.opts.BeforeStart {
    56  		if err := fn(); err != nil {
    57  			return err
    58  		}
    59  	}
    60  
    61  	if err := s.opts.Server.Start(); err != nil {
    62  		return err
    63  	}
    64  
    65  	for _, fn := range s.opts.AfterStart {
    66  		if err := fn(); err != nil {
    67  			return err
    68  		}
    69  	}
    70  
    71  	return nil
    72  }
    73  
    74  func (s *mucpService) Stop() error {
    75  	var gerr error
    76  
    77  	for _, fn := range s.opts.BeforeStop {
    78  		if err := fn(); err != nil {
    79  			gerr = err
    80  		}
    81  	}
    82  
    83  	if err := s.opts.Server.Stop(); err != nil {
    84  		return err
    85  	}
    86  
    87  	for _, fn := range s.opts.AfterStop {
    88  		if err := fn(); err != nil {
    89  			gerr = err
    90  		}
    91  	}
    92  
    93  	return gerr
    94  }
    95  
    96  func (s *mucpService) Run() error {
    97  	if err := s.Start(); err != nil {
    98  		return err
    99  	}
   100  
   101  	// wait on context cancel
   102  	<-s.opts.Context.Done()
   103  
   104  	return s.Stop()
   105  }
   106  
   107  // NewService returns a new mucp service
   108  func NewService(opts ...service.Option) service.Service {
   109  	options := []service.Option{
   110  		service.Client(cmucp.NewClient()),
   111  		service.Server(smucp.NewServer()),
   112  	}
   113  
   114  	options = append(options, opts...)
   115  
   116  	return newService(options...)
   117  }