github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/options.go (about)

     1  /*
     2   * Copyright 2023 Wang Min Xiang
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   * 	http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   */
    17  
    18  package fns
    19  
    20  import (
    21  	"fmt"
    22  	"github.com/aacfactory/configures"
    23  	"github.com/aacfactory/fns/commons/bytex"
    24  	"github.com/aacfactory/fns/commons/versions"
    25  	"github.com/aacfactory/fns/configs"
    26  	"github.com/aacfactory/fns/hooks"
    27  	"github.com/aacfactory/fns/logs"
    28  	"github.com/aacfactory/fns/proxies"
    29  	"github.com/aacfactory/fns/services/validators"
    30  	"github.com/aacfactory/fns/transports"
    31  	"github.com/aacfactory/fns/transports/fast"
    32  	"os"
    33  	"strings"
    34  	"time"
    35  )
    36  
    37  // +-------------------------------------------------------------------------------------------------------------------+
    38  
    39  type Option func(*Options) error
    40  
    41  var (
    42  	defaultOptions = &Options{
    43  		id:                    "",
    44  		name:                  "fns",
    45  		version:               versions.New(0, 0, 1),
    46  		configRetrieverOption: configs.DefaultConfigRetrieverOption(),
    47  		logWriters:            nil,
    48  		transport:             fast.New(),
    49  		middlewares:           make([]transports.Middleware, 0, 1),
    50  		handlers:              make([]transports.MuxHandler, 0, 1),
    51  		hooks:                 nil,
    52  		shutdownTimeout:       60 * time.Second,
    53  		proxyOptions:          make([]proxies.Option, 0, 1),
    54  	}
    55  )
    56  
    57  // +-------------------------------------------------------------------------------------------------------------------+
    58  
    59  type Options struct {
    60  	id                    string
    61  	name                  string
    62  	version               versions.Version
    63  	configRetrieverOption configures.RetrieverOption
    64  	logWriters            []logs.Writer
    65  	transport             transports.Transport
    66  	middlewares           []transports.Middleware
    67  	handlers              []transports.MuxHandler
    68  	hooks                 []hooks.Hook
    69  	shutdownTimeout       time.Duration
    70  	proxyOptions          []proxies.Option
    71  }
    72  
    73  // +-------------------------------------------------------------------------------------------------------------------+
    74  
    75  func ConfigRetriever(path string, format string, active string, prefix string, splitter byte) Option {
    76  	return func(o *Options) error {
    77  		path = strings.TrimSpace(path)
    78  		if path == "" {
    79  			return fmt.Errorf("path is empty")
    80  		}
    81  		active = strings.TrimSpace(active)
    82  		format = strings.ToUpper(strings.TrimSpace(format))
    83  		store := configures.NewFileStore(path, prefix, splitter)
    84  		o.configRetrieverOption = configures.RetrieverOption{
    85  			Active: active,
    86  			Format: format,
    87  			Store:  store,
    88  		}
    89  		return nil
    90  	}
    91  }
    92  
    93  func ConfigActiveFromENV(key string) (active string) {
    94  	v, has := os.LookupEnv(key)
    95  	if !has {
    96  		return
    97  	}
    98  	active = strings.ToLower(strings.TrimSpace(v))
    99  	return
   100  }
   101  
   102  // +-------------------------------------------------------------------------------------------------------------------+
   103  
   104  func Id(id string) Option {
   105  	return func(options *Options) error {
   106  		id = strings.TrimSpace(id)
   107  		if id == "" {
   108  			return fmt.Errorf("customize id failed for empty")
   109  		}
   110  		options.id = id
   111  		return nil
   112  	}
   113  }
   114  
   115  func Name(name string) Option {
   116  	return func(options *Options) error {
   117  		name = strings.TrimSpace(name)
   118  		if name == "" {
   119  			return fmt.Errorf("customize name failed for empty")
   120  		}
   121  		options.name = name
   122  		return nil
   123  	}
   124  }
   125  
   126  func Version(version string) Option {
   127  	return func(options *Options) error {
   128  		version = strings.TrimSpace(version)
   129  		if version == "" {
   130  			return fmt.Errorf("customize version failed for empty")
   131  		}
   132  		ver, parseErr := versions.Parse(bytex.FromString(version))
   133  		if parseErr != nil {
   134  			return parseErr
   135  		}
   136  		options.version = ver
   137  		return nil
   138  	}
   139  }
   140  
   141  // +-------------------------------------------------------------------------------------------------------------------+
   142  
   143  func RegisterValidator(register validators.ValidateRegister) Option {
   144  	return func(options *Options) error {
   145  		if register == nil {
   146  			return fmt.Errorf("customize validator failed for nil")
   147  		}
   148  		validators.AddValidateRegister(register)
   149  		return nil
   150  	}
   151  }
   152  
   153  // +-------------------------------------------------------------------------------------------------------------------+
   154  
   155  func Hooks(h ...hooks.Hook) Option {
   156  	return func(options *Options) error {
   157  		if h == nil || len(h) == 0 {
   158  			return fmt.Errorf("customize hooks failed for nil")
   159  		}
   160  		if options.hooks == nil {
   161  			options.hooks = make([]hooks.Hook, 0, 1)
   162  		}
   163  		for _, hook := range h {
   164  			if hook == nil {
   165  				continue
   166  			}
   167  			options.hooks = append(options.hooks, hook)
   168  		}
   169  		return nil
   170  	}
   171  }
   172  
   173  // +-------------------------------------------------------------------------------------------------------------------+
   174  
   175  func LogWriters(writers ...logs.Writer) Option {
   176  	return func(options *Options) error {
   177  		options.logWriters = append(options.logWriters, writers...)
   178  		return nil
   179  	}
   180  }
   181  
   182  // +-------------------------------------------------------------------------------------------------------------------+
   183  
   184  func ShutdownTimeout(timeout time.Duration) Option {
   185  	return func(options *Options) error {
   186  		if timeout < 1 {
   187  			return fmt.Errorf("customize application shutdown timeout failed for nil")
   188  		}
   189  		options.shutdownTimeout = timeout
   190  		return nil
   191  	}
   192  }
   193  
   194  // +-------------------------------------------------------------------------------------------------------------------+
   195  
   196  func Transport(transport transports.Transport) Option {
   197  	return func(options *Options) error {
   198  		options.transport = transport
   199  		return nil
   200  	}
   201  }
   202  
   203  func Middleware(middleware transports.Middleware) Option {
   204  	return func(options *Options) error {
   205  		options.middlewares = append(options.middlewares, middleware)
   206  		return nil
   207  	}
   208  }
   209  
   210  func Handler(handler transports.MuxHandler) Option {
   211  	return func(options *Options) error {
   212  		options.handlers = append(options.handlers, handler)
   213  		return nil
   214  	}
   215  }
   216  
   217  // +-------------------------------------------------------------------------------------------------------------------+
   218  
   219  func Proxy(options ...proxies.Option) Option {
   220  	return func(opts *Options) error {
   221  		opts.proxyOptions = append(opts.proxyOptions, options...)
   222  		return nil
   223  	}
   224  }