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

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