github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/transports/config.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 transports
    19  
    20  import (
    21  	"fmt"
    22  	"github.com/aacfactory/configures"
    23  	"github.com/aacfactory/errors"
    24  	"github.com/aacfactory/fns/transports/ssl"
    25  	"github.com/aacfactory/json"
    26  	"strings"
    27  )
    28  
    29  func FixedTLSConfig(conf ssl.Config) *TLSConfig {
    30  	return &TLSConfig{
    31  		conf: conf,
    32  	}
    33  }
    34  
    35  type TLSConfig struct {
    36  	// Kind
    37  	// ACME
    38  	// SSC(SELF-SIGN-CERT)
    39  	// DEFAULT
    40  	Kind    string          `json:"kind" yaml:"kind,omitempty"`
    41  	Options json.RawMessage `json:"options" yaml:"options,omitempty"`
    42  	conf    ssl.Config
    43  }
    44  
    45  func (config *TLSConfig) Config() (conf ssl.Config, err error) {
    46  	if config.conf != nil {
    47  		conf = config.conf
    48  		return
    49  	}
    50  	kind := strings.TrimSpace(config.Kind)
    51  	hasConf := false
    52  	conf, hasConf = ssl.GetConfig(kind)
    53  	if !hasConf {
    54  		err = errors.Warning(fmt.Sprintf("fns: can not get %s tls config", kind))
    55  		return
    56  	}
    57  	if len(config.Options) == 0 {
    58  		config.Options = []byte{'{', '}'}
    59  	}
    60  	confOptions, confOptionsErr := configures.NewJsonConfig(config.Options)
    61  	if confOptionsErr != nil {
    62  		err = errors.Warning(fmt.Sprintf("fns: can not get options of %s tls config", kind)).WithCause(confOptionsErr)
    63  		return
    64  	}
    65  	err = conf.Construct(confOptions)
    66  	return
    67  }
    68  
    69  type Config struct {
    70  	Port        int             `json:"port,omitempty" yaml:"port,omitempty"`
    71  	TLS         *TLSConfig      `json:"tls,omitempty" yaml:"tls,omitempty"`
    72  	Options     json.RawMessage `json:"options,omitempty" yaml:"options,omitempty"`
    73  	Middlewares json.RawMessage `json:"middlewares,omitempty" yaml:"middlewares,omitempty"`
    74  	Handlers    json.RawMessage `json:"handlers,omitempty" yaml:"handlers,omitempty"`
    75  }
    76  
    77  func (config *Config) GetPort() (port int, err error) {
    78  	port = config.Port
    79  	if port == 0 {
    80  		if config.TLS == nil {
    81  			port = 80
    82  		} else {
    83  			port = 443
    84  		}
    85  	}
    86  	if port < 1 || port > 65535 {
    87  		err = errors.Warning("port is invalid, port must great than 1024 or less than 65536")
    88  		return
    89  	}
    90  	return
    91  }
    92  
    93  func (config *Config) GetTLS() (tls ssl.Config, err error) {
    94  	if config.TLS == nil {
    95  		return
    96  	}
    97  	tls, err = config.TLS.Config()
    98  	if err != nil {
    99  		err = errors.Warning("tls is invalid").WithCause(err)
   100  		return
   101  	}
   102  	return
   103  }
   104  
   105  func (config *Config) OptionsConfig() (options configures.Config, err error) {
   106  	if len(config.Options) == 0 {
   107  		config.Options = []byte{'{', '}'}
   108  	}
   109  	options, err = configures.NewJsonConfig(config.Options)
   110  	if err != nil {
   111  		err = errors.Warning("options is invalid").WithCause(err)
   112  		return
   113  	}
   114  	return
   115  }
   116  
   117  func (config *Config) MiddlewareConfig(name string) (middleware configures.Config, err error) {
   118  	name = strings.TrimSpace(name)
   119  	if name == "" {
   120  		err = errors.Warning("middleware is invalid").WithCause(fmt.Errorf("name is nil")).WithMeta("middleware", name)
   121  		return
   122  	}
   123  	if len(config.Middlewares) == 0 {
   124  		config.Middlewares = []byte{'{', '}'}
   125  	}
   126  	middlewares, middlewaresErr := configures.NewJsonConfig(config.Middlewares)
   127  	if middlewaresErr != nil {
   128  		err = errors.Warning("middleware is invalid").WithCause(middlewaresErr).WithMeta("middleware", name)
   129  		return
   130  	}
   131  	has := false
   132  	middleware, has = middlewares.Node(name)
   133  	if !has {
   134  		middleware, _ = configures.NewJsonConfig([]byte{'{', '}'})
   135  	}
   136  	return
   137  }
   138  
   139  func (config *Config) HandlerConfig(name string) (handler configures.Config, err error) {
   140  	name = strings.TrimSpace(name)
   141  	if name == "" {
   142  		err = errors.Warning("middleware is invalid").WithCause(fmt.Errorf("name is nil")).WithMeta("middleware", name)
   143  		return
   144  	}
   145  	if len(config.Handlers) == 0 {
   146  		config.Handlers = []byte{'{', '}'}
   147  	}
   148  	handlers, handlersErr := configures.NewJsonConfig(config.Handlers)
   149  	if handlersErr != nil {
   150  		err = errors.Warning("middleware is invalid").WithCause(handlersErr).WithMeta("middleware", name)
   151  		return
   152  	}
   153  	has := false
   154  	handler, has = handlers.Node(name)
   155  	if !has {
   156  		handler, _ = configures.NewJsonConfig([]byte{'{', '}'})
   157  	}
   158  	return
   159  }