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

     1  package server
     2  
     3  import (
     4  	"context"
     5  	"crypto/tls"
     6  	"sync"
     7  	"time"
     8  
     9  	"go-micro.dev/v5/broker"
    10  	"go-micro.dev/v5/codec"
    11  	"go-micro.dev/v5/debug/trace"
    12  	"go-micro.dev/v5/logger"
    13  	"go-micro.dev/v5/registry"
    14  	"go-micro.dev/v5/transport"
    15  )
    16  
    17  type RouterOptions struct {
    18  	Logger logger.Logger
    19  }
    20  
    21  type RouterOption func(o *RouterOptions)
    22  
    23  func NewRouterOptions(opt ...RouterOption) RouterOptions {
    24  	opts := RouterOptions{
    25  		Logger: logger.DefaultLogger,
    26  	}
    27  
    28  	for _, o := range opt {
    29  		o(&opts)
    30  	}
    31  
    32  	return opts
    33  }
    34  
    35  // WithRouterLogger sets the underline router logger.
    36  func WithRouterLogger(l logger.Logger) RouterOption {
    37  	return func(o *RouterOptions) {
    38  		o.Logger = l
    39  	}
    40  }
    41  
    42  type Options struct {
    43  	Logger logger.Logger
    44  
    45  	Broker    broker.Broker
    46  	Registry  registry.Registry
    47  	Tracer    trace.Tracer
    48  	Transport transport.Transport
    49  
    50  	// Other options for implementations of the interface
    51  	// can be stored in a context
    52  	Context context.Context
    53  
    54  	// The router for requests
    55  	Router Router
    56  
    57  	// RegisterCheck runs a check function before registering the service
    58  	RegisterCheck func(context.Context) error
    59  	Metadata      map[string]string
    60  
    61  	// TLSConfig specifies tls.Config for secure serving
    62  	TLSConfig *tls.Config
    63  
    64  	Codecs        map[string]codec.NewCodec
    65  	Name          string
    66  	Id            string
    67  	Version       string
    68  	Advertise     string
    69  	Address       string
    70  	HdlrWrappers  []HandlerWrapper
    71  	ListenOptions []transport.ListenOption
    72  	SubWrappers   []SubscriberWrapper
    73  	// The interval on which to register
    74  	RegisterInterval time.Duration
    75  
    76  	// The register expiry time
    77  	RegisterTTL time.Duration
    78  }
    79  
    80  // NewOptions creates new server options.
    81  func NewOptions(opt ...Option) Options {
    82  	opts := Options{
    83  		Codecs:           make(map[string]codec.NewCodec),
    84  		Metadata:         map[string]string{},
    85  		RegisterInterval: DefaultRegisterInterval,
    86  		RegisterTTL:      DefaultRegisterTTL,
    87  		Logger:           logger.DefaultLogger,
    88  	}
    89  
    90  	for _, o := range opt {
    91  		o(&opts)
    92  	}
    93  
    94  	if opts.Broker == nil {
    95  		opts.Broker = broker.DefaultBroker
    96  	}
    97  
    98  	if opts.Registry == nil {
    99  		opts.Registry = registry.DefaultRegistry
   100  	}
   101  
   102  	if opts.Transport == nil {
   103  		opts.Transport = transport.DefaultTransport
   104  	}
   105  
   106  	if opts.RegisterCheck == nil {
   107  		opts.RegisterCheck = DefaultRegisterCheck
   108  	}
   109  
   110  	if len(opts.Address) == 0 {
   111  		opts.Address = DefaultAddress
   112  	}
   113  
   114  	if len(opts.Name) == 0 {
   115  		opts.Name = DefaultName
   116  	}
   117  
   118  	if len(opts.Id) == 0 {
   119  		opts.Id = DefaultId
   120  	}
   121  
   122  	if len(opts.Version) == 0 {
   123  		opts.Version = DefaultVersion
   124  	}
   125  
   126  	return opts
   127  }
   128  
   129  // Server name.
   130  func Name(n string) Option {
   131  	return func(o *Options) {
   132  		o.Name = n
   133  	}
   134  }
   135  
   136  // Unique server id.
   137  func Id(id string) Option {
   138  	return func(o *Options) {
   139  		o.Id = id
   140  	}
   141  }
   142  
   143  // Version of the service.
   144  func Version(v string) Option {
   145  	return func(o *Options) {
   146  		o.Version = v
   147  	}
   148  }
   149  
   150  // Address to bind to - host:port.
   151  func Address(a string) Option {
   152  	return func(o *Options) {
   153  		o.Address = a
   154  	}
   155  }
   156  
   157  // The address to advertise for discovery - host:port.
   158  func Advertise(a string) Option {
   159  	return func(o *Options) {
   160  		o.Advertise = a
   161  	}
   162  }
   163  
   164  // Broker to use for pub/sub.
   165  func Broker(b broker.Broker) Option {
   166  	return func(o *Options) {
   167  		o.Broker = b
   168  	}
   169  }
   170  
   171  // Codec to use to encode/decode requests for a given content type.
   172  func Codec(contentType string, c codec.NewCodec) Option {
   173  	return func(o *Options) {
   174  		o.Codecs[contentType] = c
   175  	}
   176  }
   177  
   178  // Context specifies a context for the service.
   179  // Can be used to signal shutdown of the service
   180  // Can be used for extra option values.
   181  func Context(ctx context.Context) Option {
   182  	return func(o *Options) {
   183  		o.Context = ctx
   184  	}
   185  }
   186  
   187  // Registry used for discovery.
   188  func Registry(r registry.Registry) Option {
   189  	return func(o *Options) {
   190  		o.Registry = r
   191  	}
   192  }
   193  
   194  // Tracer mechanism for distributed tracking.
   195  func Tracer(t trace.Tracer) Option {
   196  	return func(o *Options) {
   197  		o.Tracer = t
   198  	}
   199  }
   200  
   201  // Transport mechanism for communication e.g http, rabbitmq, etc.
   202  func Transport(t transport.Transport) Option {
   203  	return func(o *Options) {
   204  		o.Transport = t
   205  	}
   206  }
   207  
   208  // Metadata associated with the server.
   209  func Metadata(md map[string]string) Option {
   210  	return func(o *Options) {
   211  		o.Metadata = md
   212  	}
   213  }
   214  
   215  // RegisterCheck run func before registry service.
   216  func RegisterCheck(fn func(context.Context) error) Option {
   217  	return func(o *Options) {
   218  		o.RegisterCheck = fn
   219  	}
   220  }
   221  
   222  // Register the service with a TTL.
   223  func RegisterTTL(t time.Duration) Option {
   224  	return func(o *Options) {
   225  		o.RegisterTTL = t
   226  	}
   227  }
   228  
   229  // Register the service with at interval.
   230  func RegisterInterval(t time.Duration) Option {
   231  	return func(o *Options) {
   232  		o.RegisterInterval = t
   233  	}
   234  }
   235  
   236  // TLSConfig specifies a *tls.Config.
   237  func TLSConfig(t *tls.Config) Option {
   238  	return func(o *Options) {
   239  		// set the internal tls
   240  		o.TLSConfig = t
   241  
   242  		// set the default transport if one is not
   243  		// already set. Required for Init call below.
   244  		if o.Transport == nil {
   245  			o.Transport = transport.DefaultTransport
   246  		}
   247  
   248  		// set the transport tls
   249  		o.Transport.Init(
   250  			transport.Secure(true),
   251  			transport.TLSConfig(t),
   252  		)
   253  	}
   254  }
   255  
   256  // WithRouter sets the request router.
   257  func WithRouter(r Router) Option {
   258  	return func(o *Options) {
   259  		o.Router = r
   260  	}
   261  }
   262  
   263  // WithLogger sets the underline logger.
   264  func WithLogger(l logger.Logger) Option {
   265  	return func(o *Options) {
   266  		o.Logger = l
   267  	}
   268  }
   269  
   270  // Wait tells the server to wait for requests to finish before exiting
   271  // If `wg` is nil, server only wait for completion of rpc handler.
   272  // For user need finer grained control, pass a concrete `wg` here, server will
   273  // wait against it on stop.
   274  func Wait(wg *sync.WaitGroup) Option {
   275  	return func(o *Options) {
   276  		if o.Context == nil {
   277  			o.Context = context.Background()
   278  		}
   279  		if wg == nil {
   280  			wg = new(sync.WaitGroup)
   281  		}
   282  		o.Context = context.WithValue(o.Context, wgKey{}, wg)
   283  	}
   284  }
   285  
   286  // Adds a handler Wrapper to a list of options passed into the server.
   287  func WrapHandler(w HandlerWrapper) Option {
   288  	return func(o *Options) {
   289  		o.HdlrWrappers = append(o.HdlrWrappers, w)
   290  	}
   291  }
   292  
   293  // Adds a subscriber Wrapper to a list of options passed into the server.
   294  func WrapSubscriber(w SubscriberWrapper) Option {
   295  	return func(o *Options) {
   296  		o.SubWrappers = append(o.SubWrappers, w)
   297  	}
   298  }
   299  
   300  // Add transport.ListenOption to the ListenOptions list, when using it, it will be passed to the
   301  // httpTransport.Listen() method.
   302  func ListenOption(option transport.ListenOption) Option {
   303  	return func(o *Options) {
   304  		o.ListenOptions = append(o.ListenOptions, option)
   305  	}
   306  }