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

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