github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/configs/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 configs
    19  
    20  import (
    21  	"fmt"
    22  	"github.com/aacfactory/errors"
    23  	"github.com/aacfactory/fns/clusters"
    24  	"github.com/aacfactory/fns/hooks"
    25  	"github.com/aacfactory/fns/logs"
    26  	"github.com/aacfactory/fns/proxies"
    27  	"github.com/aacfactory/fns/services"
    28  	"github.com/aacfactory/fns/shareds"
    29  	"github.com/aacfactory/fns/transports"
    30  	"github.com/aacfactory/json"
    31  )
    32  
    33  type WorkersConfig struct {
    34  	Max            int `json:"max" yaml:"max,omitempty"`
    35  	MaxIdleSeconds int `json:"maxIdleSeconds" yaml:"maxIdleSeconds,omitempty"`
    36  }
    37  
    38  type ProcsConfig struct {
    39  	Min int `json:"min" yaml:"min,omitempty"`
    40  }
    41  
    42  type RuntimeConfig struct {
    43  	Procs   ProcsConfig               `json:"procs,omitempty" yaml:"procs,omitempty"`
    44  	Workers WorkersConfig             `json:"workers,omitempty" yaml:"workers,omitempty"`
    45  	Shared  shareds.LocalSharedConfig `json:"shared,omitempty" yaml:"shared,omitempty"`
    46  }
    47  
    48  type Config struct {
    49  	Runtime   RuntimeConfig     `json:"runtime,omitempty" yaml:"runtime,omitempty"`
    50  	Log       logs.Config       `json:"log,omitempty" yaml:"log,omitempty"`
    51  	Cluster   clusters.Config   `json:"cluster,omitempty" yaml:"cluster,omitempty"`
    52  	Transport transports.Config `json:"transport,omitempty" yaml:"transport,omitempty"`
    53  	Proxy     proxies.Config    `json:"proxy,omitempty" yaml:"proxy,omitempty"`
    54  	Services  services.Config   `json:"services,omitempty" yaml:"services,omitempty"`
    55  	Hooks     hooks.Config      `json:"hooks,omitempty" yaml:"hooks,omitempty"`
    56  }
    57  
    58  func (config Config) AddService(name string, conf any) Config {
    59  	p, encodeErr := json.Marshal(conf)
    60  	if encodeErr != nil {
    61  		panic(fmt.Sprintf("%+v", errors.Warning("fns: config add service failed").WithMeta("service", name).WithCause(encodeErr)))
    62  		return config
    63  	}
    64  	if config.Services == nil {
    65  		config.Services = make(services.Config)
    66  	}
    67  	config.Services[name] = p
    68  	return config
    69  }
    70  
    71  func (config Config) SetCluster(cluster clusters.Config) Config {
    72  	config.Cluster = cluster
    73  	return config
    74  }
    75  
    76  func (config Config) SetTransport(transport transports.Config) Config {
    77  	config.Transport = transport
    78  	return config
    79  }
    80  
    81  func (config Config) SetLoggerLevel(level logs.Level) Config {
    82  	config.Log.Level = level
    83  	return config
    84  }
    85  
    86  func (config Config) SetConsoleFormatter(formatter logs.ConsoleFormatter) Config {
    87  	config.Log.Formatter = formatter
    88  	return config
    89  }
    90  
    91  func (config Config) SetConsole(out logs.ConsoleWriterOutType) Config {
    92  	config.Log.Console = out
    93  	return config
    94  }
    95  
    96  func New() Config {
    97  	return Config{
    98  		Runtime: RuntimeConfig{
    99  			Procs:   ProcsConfig{},
   100  			Workers: WorkersConfig{},
   101  			Shared:  shareds.LocalSharedConfig{},
   102  		},
   103  		Log: logs.Config{
   104  			Level:           logs.Debug,
   105  			Formatter:       logs.TextConsoleFormatter,
   106  			Console:         logs.Stdout,
   107  			DisableConsole:  false,
   108  			Consumes:        0,
   109  			Buffer:          0,
   110  			SendTimeout:     "",
   111  			ShutdownTimeout: "",
   112  			Writers:         nil,
   113  		},
   114  		Cluster: clusters.Config{},
   115  		Transport: transports.Config{
   116  			Port: 18080,
   117  		},
   118  		Proxy:    proxies.Config{},
   119  		Services: make(services.Config),
   120  		Hooks:    nil,
   121  	}
   122  }