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

     1  // Licensed under the Apache License, Version 2.0 (the "License");
     2  // you may not use this file except in compliance with the License.
     3  // You may obtain a copy of the License at
     4  //
     5  //     https://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  //
    13  // Original source: github.com/micro/go-micro/v3/registry/options.go
    14  
    15  package registry
    16  
    17  import (
    18  	"context"
    19  	"crypto/tls"
    20  	"time"
    21  )
    22  
    23  type Option func(*Options)
    24  
    25  type RegisterOption func(*RegisterOptions)
    26  
    27  type WatchOption func(*WatchOptions)
    28  
    29  type DeregisterOption func(*DeregisterOptions)
    30  
    31  type GetOption func(*GetOptions)
    32  
    33  type ListOption func(*ListOptions)
    34  
    35  type Options struct {
    36  	Addrs     []string
    37  	Timeout   time.Duration
    38  	Secure    bool
    39  	TLSConfig *tls.Config
    40  	// Other options for implementations of the interface
    41  	// can be stored in a context
    42  	Context context.Context
    43  }
    44  
    45  type RegisterOptions struct {
    46  	TTL time.Duration
    47  	// Other options for implementations of the interface
    48  	// can be stored in a context
    49  	Context context.Context
    50  	// Domain to register the service in
    51  	Domain string
    52  }
    53  
    54  type WatchOptions struct {
    55  	// Specify a service to watch
    56  	// If blank, the watch is for all services
    57  	Service string
    58  	// Other options for implementations of the interface
    59  	// can be stored in a context
    60  	Context context.Context
    61  	// Domain to watch
    62  	Domain string
    63  }
    64  
    65  type DeregisterOptions struct {
    66  	Context context.Context
    67  	// Domain the service was registered in
    68  	Domain string
    69  }
    70  
    71  type GetOptions struct {
    72  	Context context.Context
    73  	// Domain to scope the request to
    74  	Domain string
    75  }
    76  
    77  type ListOptions struct {
    78  	Context context.Context
    79  	// Domain to scope the request to
    80  	Domain string
    81  }
    82  
    83  // Addrs is the registry addresses to use
    84  func Addrs(addrs ...string) Option {
    85  	return func(o *Options) {
    86  		o.Addrs = addrs
    87  	}
    88  }
    89  
    90  func Timeout(t time.Duration) Option {
    91  	return func(o *Options) {
    92  		o.Timeout = t
    93  	}
    94  }
    95  
    96  // Secure communication with the registry
    97  func Secure(b bool) Option {
    98  	return func(o *Options) {
    99  		o.Secure = b
   100  	}
   101  }
   102  
   103  // Specify TLS Config
   104  func TLSConfig(t *tls.Config) Option {
   105  	return func(o *Options) {
   106  		o.TLSConfig = t
   107  	}
   108  }
   109  
   110  func RegisterTTL(t time.Duration) RegisterOption {
   111  	return func(o *RegisterOptions) {
   112  		o.TTL = t
   113  	}
   114  }
   115  
   116  func RegisterContext(ctx context.Context) RegisterOption {
   117  	return func(o *RegisterOptions) {
   118  		o.Context = ctx
   119  	}
   120  }
   121  
   122  func RegisterDomain(d string) RegisterOption {
   123  	return func(o *RegisterOptions) {
   124  		o.Domain = d
   125  	}
   126  }
   127  
   128  // Watch a service
   129  func WatchService(name string) WatchOption {
   130  	return func(o *WatchOptions) {
   131  		o.Service = name
   132  	}
   133  }
   134  
   135  func WatchContext(ctx context.Context) WatchOption {
   136  	return func(o *WatchOptions) {
   137  		o.Context = ctx
   138  	}
   139  }
   140  
   141  func WatchDomain(d string) WatchOption {
   142  	return func(o *WatchOptions) {
   143  		o.Domain = d
   144  	}
   145  }
   146  
   147  func DeregisterContext(ctx context.Context) DeregisterOption {
   148  	return func(o *DeregisterOptions) {
   149  		o.Context = ctx
   150  	}
   151  }
   152  
   153  func DeregisterDomain(d string) DeregisterOption {
   154  	return func(o *DeregisterOptions) {
   155  		o.Domain = d
   156  	}
   157  }
   158  
   159  func GetContext(ctx context.Context) GetOption {
   160  	return func(o *GetOptions) {
   161  		o.Context = ctx
   162  	}
   163  }
   164  
   165  func GetDomain(d string) GetOption {
   166  	return func(o *GetOptions) {
   167  		o.Domain = d
   168  	}
   169  }
   170  
   171  func ListContext(ctx context.Context) ListOption {
   172  	return func(o *ListOptions) {
   173  		o.Context = ctx
   174  	}
   175  }
   176  
   177  func ListDomain(d string) ListOption {
   178  	return func(o *ListOptions) {
   179  		o.Domain = d
   180  	}
   181  }