github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/network/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/network/options.go
    14  
    15  package network
    16  
    17  import (
    18  	"github.com/google/uuid"
    19  	"github.com/tickoalcantara12/micro/v3/service/network/tunnel"
    20  	tmucp "github.com/tickoalcantara12/micro/v3/service/network/tunnel/mucp"
    21  	"github.com/tickoalcantara12/micro/v3/service/proxy"
    22  	"github.com/tickoalcantara12/micro/v3/service/proxy/mucp"
    23  	"github.com/tickoalcantara12/micro/v3/service/router"
    24  	regRouter "github.com/tickoalcantara12/micro/v3/service/router/registry"
    25  )
    26  
    27  type Option func(*Options)
    28  
    29  // Options configure network
    30  type Options struct {
    31  	// Id of the node
    32  	Id string
    33  	// Name of the network
    34  	Name string
    35  	// Address to bind to
    36  	Address string
    37  	// Advertise sets the address to advertise
    38  	Advertise string
    39  	// Nodes is a list of nodes to connect to
    40  	Nodes []string
    41  	// Tunnel is network tunnel
    42  	Tunnel tunnel.Tunnel
    43  	// Router is network router
    44  	Router router.Router
    45  	// Proxy is network proxy
    46  	Proxy proxy.Proxy
    47  }
    48  
    49  // Id sets the id of the network node
    50  func Id(id string) Option {
    51  	return func(o *Options) {
    52  		o.Id = id
    53  	}
    54  }
    55  
    56  // Name sets the network name
    57  func Name(n string) Option {
    58  	return func(o *Options) {
    59  		o.Name = n
    60  	}
    61  }
    62  
    63  // Address sets the network address
    64  func Address(a string) Option {
    65  	return func(o *Options) {
    66  		o.Address = a
    67  	}
    68  }
    69  
    70  // Advertise sets the address to advertise
    71  func Advertise(a string) Option {
    72  	return func(o *Options) {
    73  		o.Advertise = a
    74  	}
    75  }
    76  
    77  // Nodes is a list of nodes to connect to
    78  func Nodes(n ...string) Option {
    79  	return func(o *Options) {
    80  		o.Nodes = n
    81  	}
    82  }
    83  
    84  // Tunnel sets the network tunnel
    85  func Tunnel(t tunnel.Tunnel) Option {
    86  	return func(o *Options) {
    87  		o.Tunnel = t
    88  	}
    89  }
    90  
    91  // Router sets the network router
    92  func Router(r router.Router) Option {
    93  	return func(o *Options) {
    94  		o.Router = r
    95  	}
    96  }
    97  
    98  // Proxy sets the network proxy
    99  func Proxy(p proxy.Proxy) Option {
   100  	return func(o *Options) {
   101  		o.Proxy = p
   102  	}
   103  }
   104  
   105  // DefaultOptions returns network default options
   106  func DefaultOptions() Options {
   107  	return Options{
   108  		Id:      uuid.New().String(),
   109  		Name:    "go.micro",
   110  		Address: ":0",
   111  		Tunnel:  tmucp.NewTunnel(),
   112  		Router:  regRouter.NewRouter(),
   113  		Proxy:   mucp.NewProxy(),
   114  	}
   115  }