gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/options.go (about)

     1  package micro
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/micro/cli"
     8  	"gitee.com/liuxuezhan/go-micro-v1.18.0/broker"
     9  	"gitee.com/liuxuezhan/go-micro-v1.18.0/client"
    10  	"gitee.com/liuxuezhan/go-micro-v1.18.0/client/selector"
    11  	"gitee.com/liuxuezhan/go-micro-v1.18.0/config/cmd"
    12  	"gitee.com/liuxuezhan/go-micro-v1.18.0/registry"
    13  	"gitee.com/liuxuezhan/go-micro-v1.18.0/server"
    14  	"gitee.com/liuxuezhan/go-micro-v1.18.0/transport"
    15  )
    16  
    17  type Options struct {
    18  	Broker    broker.Broker
    19  	Cmd       cmd.Cmd
    20  	Client    client.Client
    21  	Server    server.Server
    22  	Registry  registry.Registry
    23  	Transport transport.Transport
    24  
    25  	// Before and After funcs
    26  	BeforeStart []func() error
    27  	BeforeStop  []func() error
    28  	AfterStart  []func() error
    29  	AfterStop   []func() error
    30  
    31  	// Other options for implementations of the interface
    32  	// can be stored in a context
    33  	Context context.Context
    34  
    35  	Signal bool
    36  }
    37  
    38  func newOptions(opts ...Option) Options {
    39  	opt := Options{
    40  		Broker:    broker.DefaultBroker,
    41  		Cmd:       cmd.DefaultCmd,
    42  		Client:    client.DefaultClient,
    43  		Server:    server.DefaultServer,
    44  		Registry:  registry.DefaultRegistry,
    45  		Transport: transport.DefaultTransport,
    46  		Context:   context.Background(),
    47  		Signal:    true,
    48  	}
    49  
    50  	for _, o := range opts {
    51  		o(&opt)
    52  	}
    53  
    54  	return opt
    55  }
    56  
    57  func Broker(b broker.Broker) Option {
    58  	return func(o *Options) {
    59  		o.Broker = b
    60  		// Update Client and Server
    61  		o.Client.Init(client.Broker(b))
    62  		o.Server.Init(server.Broker(b))
    63  	}
    64  }
    65  
    66  func Cmd(c cmd.Cmd) Option {
    67  	return func(o *Options) {
    68  		o.Cmd = c
    69  	}
    70  }
    71  
    72  func Client(c client.Client) Option {
    73  	return func(o *Options) {
    74  		o.Client = c
    75  	}
    76  }
    77  
    78  // Context specifies a context for the service.
    79  // Can be used to signal shutdown of the service.
    80  // Can be used for extra option values.
    81  func Context(ctx context.Context) Option {
    82  	return func(o *Options) {
    83  		o.Context = ctx
    84  	}
    85  }
    86  
    87  // HandleSignal toggles automatic installation of the signal handler that
    88  // traps TERM, INT, and QUIT.  Users of this feature to disable the signal
    89  // handler, should control liveness of the service through the context.
    90  func HandleSignal(b bool) Option {
    91  	return func(o *Options) {
    92  		o.Signal = b
    93  	}
    94  }
    95  
    96  func Server(s server.Server) Option {
    97  	return func(o *Options) {
    98  		o.Server = s
    99  	}
   100  }
   101  
   102  // Registry sets the registry for the service
   103  // and the underlying components
   104  func Registry(r registry.Registry) Option {
   105  	return func(o *Options) {
   106  		o.Registry = r
   107  		// Update Client and Server
   108  		o.Client.Init(client.Registry(r))
   109  		o.Server.Init(server.Registry(r))
   110  		// Update Selector
   111  		o.Client.Options().Selector.Init(selector.Registry(r))
   112  		// Update Broker
   113  		o.Broker.Init(broker.Registry(r))
   114  	}
   115  }
   116  
   117  // Selector sets the selector for the service client
   118  func Selector(s selector.Selector) Option {
   119  	return func(o *Options) {
   120  		o.Client.Init(client.Selector(s))
   121  	}
   122  }
   123  
   124  // Transport sets the transport for the service
   125  // and the underlying components
   126  func Transport(t transport.Transport) Option {
   127  	return func(o *Options) {
   128  		o.Transport = t
   129  		// Update Client and Server
   130  		o.Client.Init(client.Transport(t))
   131  		o.Server.Init(server.Transport(t))
   132  	}
   133  }
   134  
   135  // Convenience options
   136  
   137  // Address sets the address of the server
   138  func Address(addr string) Option {
   139  	return func(o *Options) {
   140  		o.Server.Init(server.Address(addr))
   141  	}
   142  }
   143  
   144  // Name of the service
   145  func Name(n string) Option {
   146  	return func(o *Options) {
   147  		o.Server.Init(server.Name(n))
   148  	}
   149  }
   150  
   151  // Version of the service
   152  func Version(v string) Option {
   153  	return func(o *Options) {
   154  		o.Server.Init(server.Version(v))
   155  	}
   156  }
   157  
   158  // Metadata associated with the service
   159  func Metadata(md map[string]string) Option {
   160  	return func(o *Options) {
   161  		o.Server.Init(server.Metadata(md))
   162  	}
   163  }
   164  
   165  func Flags(flags ...cli.Flag) Option {
   166  	return func(o *Options) {
   167  		o.Cmd.App().Flags = append(o.Cmd.App().Flags, flags...)
   168  	}
   169  }
   170  
   171  func Action(a func(*cli.Context)) Option {
   172  	return func(o *Options) {
   173  		o.Cmd.App().Action = a
   174  	}
   175  }
   176  
   177  // RegisterTTL specifies the TTL to use when registering the service
   178  func RegisterTTL(t time.Duration) Option {
   179  	return func(o *Options) {
   180  		o.Server.Init(server.RegisterTTL(t))
   181  	}
   182  }
   183  
   184  // RegisterInterval specifies the interval on which to re-register
   185  func RegisterInterval(t time.Duration) Option {
   186  	return func(o *Options) {
   187  		o.Server.Init(server.RegisterInterval(t))
   188  	}
   189  }
   190  
   191  // WrapClient is a convenience method for wrapping a Client with
   192  // some middleware component. A list of wrappers can be provided.
   193  // Wrappers are applied in reverse order so the last is executed first.
   194  func WrapClient(w ...client.Wrapper) Option {
   195  	return func(o *Options) {
   196  		// apply in reverse
   197  		for i := len(w); i > 0; i-- {
   198  			o.Client = w[i-1](o.Client)
   199  		}
   200  	}
   201  }
   202  
   203  // WrapCall is a convenience method for wrapping a Client CallFunc
   204  func WrapCall(w ...client.CallWrapper) Option {
   205  	return func(o *Options) {
   206  		o.Client.Init(client.WrapCall(w...))
   207  	}
   208  }
   209  
   210  // WrapHandler adds a handler Wrapper to a list of options passed into the server
   211  func WrapHandler(w ...server.HandlerWrapper) Option {
   212  	return func(o *Options) {
   213  		var wrappers []server.Option
   214  
   215  		for _, wrap := range w {
   216  			wrappers = append(wrappers, server.WrapHandler(wrap))
   217  		}
   218  
   219  		// Init once
   220  		o.Server.Init(wrappers...)
   221  	}
   222  }
   223  
   224  // WrapSubscriber adds a subscriber Wrapper to a list of options passed into the server
   225  func WrapSubscriber(w ...server.SubscriberWrapper) Option {
   226  	return func(o *Options) {
   227  		var wrappers []server.Option
   228  
   229  		for _, wrap := range w {
   230  			wrappers = append(wrappers, server.WrapSubscriber(wrap))
   231  		}
   232  
   233  		// Init once
   234  		o.Server.Init(wrappers...)
   235  	}
   236  }
   237  
   238  // Before and Afters
   239  
   240  func BeforeStart(fn func() error) Option {
   241  	return func(o *Options) {
   242  		o.BeforeStart = append(o.BeforeStart, fn)
   243  	}
   244  }
   245  
   246  func BeforeStop(fn func() error) Option {
   247  	return func(o *Options) {
   248  		o.BeforeStop = append(o.BeforeStop, fn)
   249  	}
   250  }
   251  
   252  func AfterStart(fn func() error) Option {
   253  	return func(o *Options) {
   254  		o.AfterStart = append(o.AfterStart, fn)
   255  	}
   256  }
   257  
   258  func AfterStop(fn func() error) Option {
   259  	return func(o *Options) {
   260  		o.AfterStop = append(o.AfterStop, fn)
   261  	}
   262  }