go-micro.dev/v5@v5.12.0/service/options.go (about)

     1  package service
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/urfave/cli/v2"
     8  	"go-micro.dev/v5/auth"
     9  	"go-micro.dev/v5/broker"
    10  	"go-micro.dev/v5/cache"
    11  	"go-micro.dev/v5/client"
    12  	"go-micro.dev/v5/cmd"
    13  	"go-micro.dev/v5/config"
    14  	"go-micro.dev/v5/debug/profile"
    15  	"go-micro.dev/v5/debug/trace"
    16  	"go-micro.dev/v5/logger"
    17  	"go-micro.dev/v5/registry"
    18  	"go-micro.dev/v5/selector"
    19  	"go-micro.dev/v5/server"
    20  	"go-micro.dev/v5/store"
    21  	"go-micro.dev/v5/transport"
    22  )
    23  
    24  // Options for micro service.
    25  type Options struct {
    26  	Registry registry.Registry
    27  	Store    store.Store
    28  	Auth     auth.Auth
    29  	Cmd      cmd.Cmd
    30  	Config   config.Config
    31  	Client   client.Client
    32  	Server   server.Server
    33  
    34  	// Other options for implementations of the interface
    35  	// can be stored in a context
    36  	Context context.Context
    37  
    38  	Cache     cache.Cache
    39  	Profile   profile.Profile
    40  	Transport transport.Transport
    41  	Logger    logger.Logger
    42  	Broker    broker.Broker
    43  	// Before and After funcs
    44  	BeforeStart []func() error
    45  	AfterStart  []func() error
    46  	AfterStop   []func() error
    47  
    48  	BeforeStop []func() error
    49  
    50  	Signal bool
    51  }
    52  
    53  type Option func(*Options)
    54  
    55  func newOptions(opts ...Option) Options {
    56  	opt := Options{
    57  		Auth:      auth.DefaultAuth,
    58  		Broker:    broker.DefaultBroker,
    59  		Cache:     cache.DefaultCache,
    60  		Cmd:       cmd.DefaultCmd,
    61  		Config:    config.DefaultConfig,
    62  		Client:    client.DefaultClient,
    63  		Server:    server.DefaultServer,
    64  		Store:     store.DefaultStore,
    65  		Registry:  registry.DefaultRegistry,
    66  		Transport: transport.DefaultTransport,
    67  		Context:   context.Background(),
    68  		Signal:    true,
    69  		Logger:    logger.DefaultLogger,
    70  	}
    71  
    72  	for _, o := range opts {
    73  		o(&opt)
    74  	}
    75  
    76  	return opt
    77  }
    78  
    79  // Broker to be used for service.
    80  func Broker(b broker.Broker) Option {
    81  	return func(o *Options) {
    82  		o.Broker = b
    83  		// Update Client and Server
    84  		o.Client.Init(client.Broker(b))
    85  		o.Server.Init(server.Broker(b))
    86  		broker.DefaultBroker = b
    87  	}
    88  }
    89  
    90  func Cache(c cache.Cache) Option {
    91  	return func(o *Options) {
    92  		o.Cache = c
    93  		cache.DefaultCache = c
    94  	}
    95  }
    96  
    97  func Cmd(c cmd.Cmd) Option {
    98  	return func(o *Options) {
    99  		o.Cmd = c
   100  	}
   101  }
   102  
   103  // Client to be used for service.
   104  func Client(c client.Client) Option {
   105  	return func(o *Options) {
   106  		o.Client = c
   107  		client.DefaultClient = c
   108  	}
   109  }
   110  
   111  // Context specifies a context for the service.
   112  // Can be used to signal shutdown of the service and for extra option values.
   113  func Context(ctx context.Context) Option {
   114  	return func(o *Options) {
   115  		o.Context = ctx
   116  	}
   117  }
   118  
   119  // Handle will register a handler without any fuss
   120  func Handle(v interface{}) Option {
   121  	return func(o *Options) {
   122  		o.Server.Handle(
   123  			o.Server.NewHandler(v),
   124  		)
   125  	}
   126  }
   127  
   128  // HandleSignal toggles automatic installation of the signal handler that
   129  // traps TERM, INT, and QUIT.  Users of this feature to disable the signal
   130  // handler, should control liveness of the service through the context.
   131  func HandleSignal(b bool) Option {
   132  	return func(o *Options) {
   133  		o.Signal = b
   134  	}
   135  }
   136  
   137  // Profile to be used for debug profile.
   138  func Profile(p profile.Profile) Option {
   139  	return func(o *Options) {
   140  		o.Profile = p
   141  		profile.DefaultProfile = p
   142  	}
   143  }
   144  
   145  // Server to be used for service.
   146  func Server(s server.Server) Option {
   147  	return func(o *Options) {
   148  		o.Server = s
   149  		server.DefaultServer = s
   150  	}
   151  }
   152  
   153  // Store sets the store to use.
   154  func Store(s store.Store) Option {
   155  	return func(o *Options) {
   156  		o.Store = s
   157  		store.DefaultStore = s
   158  	}
   159  }
   160  
   161  // Registry sets the registry for the service
   162  // and the underlying components.
   163  func Registry(r registry.Registry) Option {
   164  	return func(o *Options) {
   165  		o.Registry = r
   166  		// Update Client and Server
   167  		o.Client.Init(client.Registry(r))
   168  		o.Server.Init(server.Registry(r))
   169  		// Update Broker
   170  		o.Broker.Init(broker.Registry(r))
   171  		broker.DefaultBroker = o.Broker
   172  	}
   173  }
   174  
   175  // Tracer sets the tracer for the service.
   176  func Tracer(t trace.Tracer) Option {
   177  	return func(o *Options) {
   178  		o.Server.Init(server.Tracer(t))
   179  	}
   180  
   181  }
   182  
   183  // Auth sets the auth for the service.
   184  func Auth(a auth.Auth) Option {
   185  	return func(o *Options) {
   186  		o.Auth = a
   187  		auth.DefaultAuth = a
   188  
   189  	}
   190  }
   191  
   192  // Config sets the config for the service.
   193  func Config(c config.Config) Option {
   194  	return func(o *Options) {
   195  		o.Config = c
   196  		config.DefaultConfig = c
   197  	}
   198  }
   199  
   200  // Selector sets the selector for the service client.
   201  func Selector(s selector.Selector) Option {
   202  	return func(o *Options) {
   203  		o.Client.Init(client.Selector(s))
   204  		selector.DefaultSelector = s
   205  	}
   206  }
   207  
   208  // Transport sets the transport for the service
   209  // and the underlying components.
   210  func Transport(t transport.Transport) Option {
   211  	return func(o *Options) {
   212  		o.Transport = t
   213  		// Update Client and Server
   214  		o.Client.Init(client.Transport(t))
   215  		o.Server.Init(server.Transport(t))
   216  		transport.DefaultTransport = t
   217  	}
   218  }
   219  
   220  // Convenience options
   221  
   222  // Address sets the address of the server.
   223  func Address(addr string) Option {
   224  	return func(o *Options) {
   225  		o.Server.Init(server.Address(addr))
   226  	}
   227  }
   228  
   229  // Name of the service.
   230  func Name(n string) Option {
   231  	return func(o *Options) {
   232  		o.Server.Init(server.Name(n))
   233  	}
   234  }
   235  
   236  // Version of the service.
   237  func Version(v string) Option {
   238  	return func(o *Options) {
   239  		o.Server.Init(server.Version(v))
   240  	}
   241  }
   242  
   243  // Metadata associated with the service.
   244  func Metadata(md map[string]string) Option {
   245  	return func(o *Options) {
   246  		o.Server.Init(server.Metadata(md))
   247  	}
   248  }
   249  
   250  // Flags that can be passed to service.
   251  func Flags(flags ...cli.Flag) Option {
   252  	return func(o *Options) {
   253  		o.Cmd.App().Flags = append(o.Cmd.App().Flags, flags...)
   254  	}
   255  }
   256  
   257  // Action can be used to parse user provided cli options.
   258  func Action(a func(*cli.Context) error) Option {
   259  	return func(o *Options) {
   260  		o.Cmd.App().Action = a
   261  	}
   262  }
   263  
   264  // RegisterTTL specifies the TTL to use when registering the service.
   265  func RegisterTTL(t time.Duration) Option {
   266  	return func(o *Options) {
   267  		o.Server.Init(server.RegisterTTL(t))
   268  	}
   269  }
   270  
   271  // RegisterInterval specifies the interval on which to re-register.
   272  func RegisterInterval(t time.Duration) Option {
   273  	return func(o *Options) {
   274  		o.Server.Init(server.RegisterInterval(t))
   275  	}
   276  }
   277  
   278  // WrapClient is a convenience method for wrapping a Client with
   279  // some middleware component. A list of wrappers can be provided.
   280  // Wrappers are applied in reverse order so the last is executed first.
   281  func WrapClient(w ...client.Wrapper) Option {
   282  	return func(o *Options) {
   283  		// apply in reverse
   284  		for i := len(w); i > 0; i-- {
   285  			o.Client = w[i-1](o.Client)
   286  		}
   287  	}
   288  }
   289  
   290  // WrapCall is a convenience method for wrapping a Client CallFunc.
   291  func WrapCall(w ...client.CallWrapper) Option {
   292  	return func(o *Options) {
   293  		o.Client.Init(client.WrapCall(w...))
   294  	}
   295  }
   296  
   297  // WrapHandler adds a handler Wrapper to a list of options passed into the server.
   298  func WrapHandler(w ...server.HandlerWrapper) Option {
   299  	return func(o *Options) {
   300  		var wrappers []server.Option
   301  
   302  		for _, wrap := range w {
   303  			wrappers = append(wrappers, server.WrapHandler(wrap))
   304  		}
   305  
   306  		// Init once
   307  		o.Server.Init(wrappers...)
   308  	}
   309  }
   310  
   311  // WrapSubscriber adds a subscriber Wrapper to a list of options passed into the server.
   312  func WrapSubscriber(w ...server.SubscriberWrapper) Option {
   313  	return func(o *Options) {
   314  		var wrappers []server.Option
   315  
   316  		for _, wrap := range w {
   317  			wrappers = append(wrappers, server.WrapSubscriber(wrap))
   318  		}
   319  
   320  		// Init once
   321  		o.Server.Init(wrappers...)
   322  	}
   323  }
   324  
   325  // Add opt to server option.
   326  func AddListenOption(option server.Option) Option {
   327  	return func(o *Options) {
   328  		o.Server.Init(option)
   329  	}
   330  }
   331  
   332  // Before and Afters
   333  
   334  // BeforeStart run funcs before service starts.
   335  func BeforeStart(fn func() error) Option {
   336  	return func(o *Options) {
   337  		o.BeforeStart = append(o.BeforeStart, fn)
   338  	}
   339  }
   340  
   341  // BeforeStop run funcs before service stops.
   342  func BeforeStop(fn func() error) Option {
   343  	return func(o *Options) {
   344  		o.BeforeStop = append(o.BeforeStop, fn)
   345  	}
   346  }
   347  
   348  // AfterStart run funcs after service starts.
   349  func AfterStart(fn func() error) Option {
   350  	return func(o *Options) {
   351  		o.AfterStart = append(o.AfterStart, fn)
   352  	}
   353  }
   354  
   355  // AfterStop run funcs after service stops.
   356  func AfterStop(fn func() error) Option {
   357  	return func(o *Options) {
   358  		o.AfterStop = append(o.AfterStop, fn)
   359  	}
   360  }
   361  
   362  // Logger sets the logger for the service.
   363  func Logger(l logger.Logger) Option {
   364  	return func(o *Options) {
   365  		o.Logger = l
   366  	}
   367  }