github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/options.go (about)

     1  package service
     2  
     3  import (
     4  	"time"
     5  
     6  	// TODO: replace with micro/v3/service/cli
     7  	"github.com/tickoalcantara12/micro/v3/cmd"
     8  	"github.com/tickoalcantara12/micro/v3/service/client"
     9  	"github.com/tickoalcantara12/micro/v3/service/server"
    10  )
    11  
    12  // Options for micro service
    13  type Options struct {
    14  	Cmd cmd.Cmd
    15  
    16  	Name    string
    17  	Version string
    18  
    19  	// Before and After funcs
    20  	BeforeStart []func() error
    21  	BeforeStop  []func() error
    22  	AfterStart  []func() error
    23  	AfterStop   []func() error
    24  
    25  	Signal bool
    26  }
    27  
    28  func newOptions(opts ...Option) Options {
    29  	opt := Options{
    30  		Cmd:    cmd.DefaultCmd,
    31  		Signal: true,
    32  	}
    33  
    34  	for _, o := range opts {
    35  		o(&opt)
    36  	}
    37  	return opt
    38  }
    39  
    40  type Option func(o *Options)
    41  
    42  // HandleSignal toggles automatic installation of the signal handler that
    43  // traps TERM, INT, and QUIT.  Users of this feature to disable the signal
    44  // handler, should control liveness of the service through the context.
    45  func HandleSignal(b bool) Option {
    46  	return func(o *Options) {
    47  		o.Signal = b
    48  	}
    49  }
    50  
    51  // Address sets the address of the server
    52  func Address(addr string) Option {
    53  	return func(o *Options) {
    54  		server.DefaultServer.Init(server.Address(addr))
    55  	}
    56  }
    57  
    58  // Name of the service
    59  func Name(n string) Option {
    60  	return func(o *Options) {
    61  		o.Name = n
    62  		server.DefaultServer.Init(server.Name(n))
    63  	}
    64  }
    65  
    66  // Version of the service
    67  func Version(v string) Option {
    68  	return func(o *Options) {
    69  		o.Version = v
    70  		server.DefaultServer.Init(server.Version(v))
    71  	}
    72  }
    73  
    74  // Metadata associated with the service
    75  func Metadata(md map[string]string) Option {
    76  	return func(o *Options) {
    77  		server.DefaultServer.Init(server.Metadata(md))
    78  	}
    79  }
    80  
    81  // RegisterTTL specifies the TTL to use when registering the service
    82  func RegisterTTL(t time.Duration) Option {
    83  	return func(o *Options) {
    84  		server.DefaultServer.Init(server.RegisterTTL(t))
    85  	}
    86  }
    87  
    88  // RegisterInterval specifies the interval on which to re-register
    89  func RegisterInterval(t time.Duration) Option {
    90  	return func(o *Options) {
    91  		server.DefaultServer.Init(server.RegisterInterval(t))
    92  	}
    93  }
    94  
    95  // WrapClient is a convenience method for wrapping a Client with
    96  // some middleware component. A list of wrappers can be provided.
    97  // Wrappers are applied in reverse order so the last is executed first.
    98  func WrapClient(w ...client.Wrapper) Option {
    99  	return func(o *Options) {
   100  		// apply in reverse
   101  		for i := len(w); i > 0; i-- {
   102  			client.DefaultClient = w[i-1](client.DefaultClient)
   103  		}
   104  	}
   105  }
   106  
   107  // WrapCall is a convenience method for wrapping a Client CallFunc
   108  func WrapCall(w ...client.CallWrapper) Option {
   109  	return func(o *Options) {
   110  		client.DefaultClient.Init(client.WrapCall(w...))
   111  	}
   112  }
   113  
   114  // WrapHandler adds a handler Wrapper to a list of options passed into the server
   115  func WrapHandler(w ...server.HandlerWrapper) Option {
   116  	return func(o *Options) {
   117  		var wrappers []server.Option
   118  
   119  		for _, wrap := range w {
   120  			wrappers = append(wrappers, server.WrapHandler(wrap))
   121  		}
   122  
   123  		// Init once
   124  		server.DefaultServer.Init(wrappers...)
   125  	}
   126  }
   127  
   128  // WrapSubscriber adds a subscriber Wrapper to a list of options passed into the server
   129  func WrapSubscriber(w ...server.SubscriberWrapper) Option {
   130  	return func(o *Options) {
   131  		var wrappers []server.Option
   132  
   133  		for _, wrap := range w {
   134  			wrappers = append(wrappers, server.WrapSubscriber(wrap))
   135  		}
   136  
   137  		// Init once
   138  		server.DefaultServer.Init(wrappers...)
   139  	}
   140  }
   141  
   142  // Before and Afters
   143  
   144  // BeforeStart run funcs before service starts
   145  func BeforeStart(fn func() error) Option {
   146  	return func(o *Options) {
   147  		o.BeforeStart = append(o.BeforeStart, fn)
   148  	}
   149  }
   150  
   151  // BeforeStop run funcs before service stops
   152  func BeforeStop(fn func() error) Option {
   153  	return func(o *Options) {
   154  		o.BeforeStop = append(o.BeforeStop, fn)
   155  	}
   156  }
   157  
   158  // AfterStart run funcs after service starts
   159  func AfterStart(fn func() error) Option {
   160  	return func(o *Options) {
   161  		o.AfterStart = append(o.AfterStart, fn)
   162  	}
   163  }
   164  
   165  // AfterStop run funcs after service stops
   166  func AfterStop(fn func() error) Option {
   167  	return func(o *Options) {
   168  		o.AfterStop = append(o.AfterStop, fn)
   169  	}
   170  }