github.com/btccom/go-micro/v2@v2.9.3/api/handler/options.go (about)

     1  package handler
     2  
     3  import (
     4  	"github.com/btccom/go-micro/v2/api/router"
     5  	"github.com/btccom/go-micro/v2/client"
     6  	"github.com/btccom/go-micro/v2/client/grpc"
     7  )
     8  
     9  var (
    10  	DefaultMaxRecvSize int64 = 1024 * 1024 * 100 // 10Mb
    11  )
    12  
    13  type Options struct {
    14  	MaxRecvSize int64
    15  	Namespace   string
    16  	Router      router.Router
    17  	Client      client.Client
    18  }
    19  
    20  type Option func(o *Options)
    21  
    22  // NewOptions fills in the blanks
    23  func NewOptions(opts ...Option) Options {
    24  	var options Options
    25  	for _, o := range opts {
    26  		o(&options)
    27  	}
    28  
    29  	if options.Client == nil {
    30  		WithClient(grpc.NewClient())(&options)
    31  	}
    32  
    33  	// set namespace if blank
    34  	if len(options.Namespace) == 0 {
    35  		WithNamespace("go.micro.api")(&options)
    36  	}
    37  
    38  	if options.MaxRecvSize == 0 {
    39  		options.MaxRecvSize = DefaultMaxRecvSize
    40  	}
    41  
    42  	return options
    43  }
    44  
    45  // WithNamespace specifies the namespace for the handler
    46  func WithNamespace(s string) Option {
    47  	return func(o *Options) {
    48  		o.Namespace = s
    49  	}
    50  }
    51  
    52  // WithRouter specifies a router to be used by the handler
    53  func WithRouter(r router.Router) Option {
    54  	return func(o *Options) {
    55  		o.Router = r
    56  	}
    57  }
    58  
    59  func WithClient(c client.Client) Option {
    60  	return func(o *Options) {
    61  		o.Client = c
    62  	}
    63  }
    64  
    65  // WithmaxRecvSize specifies max body size
    66  func WithMaxRecvSize(size int64) Option {
    67  	return func(o *Options) {
    68  		o.MaxRecvSize = size
    69  	}
    70  }