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

     1  // Copyright 2020 Asim Aslam
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // Original source: github.com/micro/go-micro/v3/api/handler/options.go
    16  
    17  package handler
    18  
    19  import (
    20  	"github.com/tickoalcantara12/micro/v3/service/api/router"
    21  	"github.com/tickoalcantara12/micro/v3/service/client"
    22  	"github.com/tickoalcantara12/micro/v3/service/client/grpc"
    23  )
    24  
    25  var (
    26  	DefaultMaxRecvSize int64 = 1024 * 1024 * 100 // 10Mb
    27  )
    28  
    29  type Options struct {
    30  	MaxRecvSize int64
    31  	Namespace   string
    32  	Router      router.Router
    33  	Client      client.Client
    34  }
    35  
    36  type Option func(o *Options)
    37  
    38  // NewOptions fills in the blanks
    39  func NewOptions(opts ...Option) Options {
    40  	var options Options
    41  	for _, o := range opts {
    42  		o(&options)
    43  	}
    44  
    45  	if options.Client == nil {
    46  		WithClient(grpc.NewClient())(&options)
    47  	}
    48  
    49  	// set namespace if blank
    50  	if len(options.Namespace) == 0 {
    51  		WithNamespace("micro")(&options)
    52  	}
    53  
    54  	if options.MaxRecvSize == 0 {
    55  		options.MaxRecvSize = DefaultMaxRecvSize
    56  	}
    57  
    58  	return options
    59  }
    60  
    61  // WithNamespace specifies the namespace for the handler
    62  func WithNamespace(s string) Option {
    63  	return func(o *Options) {
    64  		o.Namespace = s
    65  	}
    66  }
    67  
    68  // WithRouter specifies a router to be used by the handler
    69  func WithRouter(r router.Router) Option {
    70  	return func(o *Options) {
    71  		o.Router = r
    72  	}
    73  }
    74  
    75  func WithClient(c client.Client) Option {
    76  	return func(o *Options) {
    77  		o.Client = c
    78  	}
    79  }
    80  
    81  // WithMaxRecvSize specifies max body size
    82  func WithMaxRecvSize(size int64) Option {
    83  	return func(o *Options) {
    84  		o.MaxRecvSize = size
    85  	}
    86  }