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

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