github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/router/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/tickoalcantara12/micro/v3/router/options.go
    14  
    15  package router
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/google/uuid"
    21  	"github.com/tickoalcantara12/micro/v3/service/registry"
    22  	"github.com/tickoalcantara12/micro/v3/service/registry/memory"
    23  )
    24  
    25  // Options are router options
    26  type Options struct {
    27  	// Id is router id
    28  	Id string
    29  	// Address is router address
    30  	Address string
    31  	// Gateway is network gateway
    32  	Gateway string
    33  	// Network is network address
    34  	Network string
    35  	// Registry is the local registry
    36  	Registry registry.Registry
    37  	// Context for additional options
    38  	Context context.Context
    39  	// Cache routes
    40  	Cache bool
    41  }
    42  
    43  // Id sets Router Id
    44  func Id(id string) Option {
    45  	return func(o *Options) {
    46  		o.Id = id
    47  	}
    48  }
    49  
    50  // Address sets router service address
    51  func Address(a string) Option {
    52  	return func(o *Options) {
    53  		o.Address = a
    54  	}
    55  }
    56  
    57  // Gateway sets network gateway
    58  func Gateway(g string) Option {
    59  	return func(o *Options) {
    60  		o.Gateway = g
    61  	}
    62  }
    63  
    64  // Network sets router network
    65  func Network(n string) Option {
    66  	return func(o *Options) {
    67  		o.Network = n
    68  	}
    69  }
    70  
    71  // Registry sets the local registry
    72  func Registry(r registry.Registry) Option {
    73  	return func(o *Options) {
    74  		o.Registry = r
    75  	}
    76  }
    77  
    78  // Cache the routes
    79  func Cache() Option {
    80  	return func(o *Options) {
    81  		o.Cache = true
    82  	}
    83  }
    84  
    85  // DefaultOptions returns router default options
    86  func DefaultOptions() Options {
    87  	return Options{
    88  		Id:       uuid.New().String(),
    89  		Network:  DefaultNetwork,
    90  		Registry: memory.NewRegistry(),
    91  		Context:  context.Background(),
    92  	}
    93  }
    94  
    95  type ReadOptions struct {
    96  	Service string
    97  }
    98  
    99  type ReadOption func(o *ReadOptions)
   100  
   101  // ReadService sets the service to read from the table
   102  func ReadService(s string) ReadOption {
   103  	return func(o *ReadOptions) {
   104  		o.Service = s
   105  	}
   106  }