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

     1  package resolver
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/annwntech/go-micro/v2/registry"
     7  )
     8  
     9  type Options struct {
    10  	Handler       string
    11  	Namespace     func(*http.Request) string
    12  	ServicePrefix string
    13  }
    14  
    15  type Option func(o *Options)
    16  
    17  // NewOptions returns new initialised options
    18  func NewOptions(opts ...Option) Options {
    19  	var options Options
    20  	for _, o := range opts {
    21  		o(&options)
    22  	}
    23  
    24  	if options.Namespace == nil {
    25  		options.Namespace = StaticNamespace("go.micro")
    26  	}
    27  
    28  	return options
    29  }
    30  
    31  // WithHandler sets the handler being used
    32  func WithHandler(h string) Option {
    33  	return func(o *Options) {
    34  		o.Handler = h
    35  	}
    36  }
    37  
    38  // WithNamespace sets the function which determines the namespace for a request
    39  func WithNamespace(n func(*http.Request) string) Option {
    40  	return func(o *Options) {
    41  		o.Namespace = n
    42  	}
    43  }
    44  
    45  // WithServicePrefix sets the ServicePrefix option
    46  func WithServicePrefix(p string) Option {
    47  	return func(o *Options) {
    48  		o.ServicePrefix = p
    49  	}
    50  }
    51  
    52  // ResolveOptions are used when resolving a request
    53  type ResolveOptions struct {
    54  	Domain string
    55  }
    56  
    57  // ResolveOption sets an option
    58  type ResolveOption func(*ResolveOptions)
    59  
    60  // Domain sets the resolve Domain option
    61  func Domain(n string) ResolveOption {
    62  	return func(o *ResolveOptions) {
    63  		o.Domain = n
    64  	}
    65  }
    66  
    67  // NewResolveOptions returns new initialised resolve options
    68  func NewResolveOptions(opts ...ResolveOption) ResolveOptions {
    69  	var options ResolveOptions
    70  	for _, o := range opts {
    71  		o(&options)
    72  	}
    73  	if len(options.Domain) == 0 {
    74  		options.Domain = registry.DefaultDomain
    75  	}
    76  
    77  	return options
    78  }