github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/config/config.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"time"
     7  
     8  	"github.com/fsnotify/fsnotify"
     9  	"github.com/mitchellh/mapstructure"
    10  	"github.com/spf13/cast"
    11  	"github.com/spf13/viper"
    12  )
    13  
    14  const (
    15  	Version             = "magnetic0.1"
    16  	DefaultKeyDir       = "keystore"
    17  	DefaultMultiAddrDir = "keystore/multiaddrdir"
    18  )
    19  
    20  var GlobalConfig MagneticConfig
    21  
    22  func InitConfig(configPath string) error {
    23  	var err error
    24  	//path := "./config.yml"
    25  	provide, err := FromConfigString(configPath, "yml")
    26  	if err != nil {
    27  		return err
    28  	}
    29  	GlobalConfig, err = DecodeConfig(provide)
    30  	if err != nil {
    31  		return err
    32  	}
    33  	err = GlobalConfig.CheckConfig([]string{"Genesis", "SysCfg", "TxPoolCfg", "P2PCfg", "CliqueCfg", "BootNode"})
    34  	if err != nil {
    35  		panic(err)
    36  	}
    37  	return nil
    38  }
    39  func (c *MagneticConfig) CheckConfig(cnames []string) error {
    40  	s := reflect.ValueOf(c).Elem()
    41  	for _, v := range cnames {
    42  		val := s.FieldByName(v)
    43  		typ := val.Type()
    44  		def := reflect.New(typ).Elem()
    45  		flag := 0
    46  		i := 0
    47  		for ; i < val.NumField(); i++ {
    48  			if val.Field(i).Kind() != reflect.Slice {
    49  				if val.Field(i).Interface() == def.Field(i).Interface() {
    50  					flag++
    51  				}
    52  			}
    53  		}
    54  		if i == flag {
    55  			return fmt.Errorf("%v is not find in config", v)
    56  		}
    57  	}
    58  	return nil
    59  }
    60  
    61  type MagneticConfig struct {
    62  	Genesis   GenesisConfig
    63  	SysCfg    SystemConfig
    64  	TxPoolCfg TxPoolConfig
    65  	P2PCfg    P2PNodeConfig
    66  	CliqueCfg CliqueConfig
    67  	BootNode  BootConfig
    68  	MongoCfg Mongo
    69  }
    70  type BootConfig struct {
    71  	IP string
    72  }
    73  type GenesisConfig struct {
    74  	ChainId   string
    75  	Crystal       uint64
    76  	Energy      uint64
    77  	Official  string
    78  	Timestamp uint64
    79  	Stars     []*StarNode
    80  	//Earth     string
    81  }
    82  type StarNode struct {
    83  	crystal     uint64
    84  	Account string
    85  	Nodekey string
    86  }
    87  
    88  type P2PRsvConfig struct {
    89  	ReservedPeers []string `json:"reserved"`
    90  	MaskPeers     []string `json:"mask"`
    91  }
    92  
    93  type P2PNodeConfig struct {
    94  	ReservedPeersOnly         bool
    95  	ReservedCfg               *P2PRsvConfig
    96  	NetworkMagic              uint32
    97  	NetworkId                 uint32
    98  	NetworkName               string
    99  	NodePort                  uint
   100  	NodeConsensusPort         uint
   101  	DualPortSupport           bool
   102  	IsTLS                     bool
   103  	CertPath                  string
   104  	KeyPath                   string
   105  	CAPath                    string
   106  	HttpInfoPort              uint
   107  	MaxHdrSyncReqs            uint
   108  	MaxConnInBound            uint
   109  	MaxConnOutBound           uint
   110  	MaxConnInBoundForSingleIP uint
   111  }
   112  
   113  type SystemConfig struct {
   114  	StoreDir string
   115  	HttpPort int
   116  	GenBlock bool
   117  	LogPath  string
   118  }
   119  
   120  type TxPoolConfig struct {
   121  	MaxPending   uint32
   122  	MaxInPending uint32
   123  	MaxInQueue   uint32
   124  	MaxTxInPool  uint32
   125  }
   126  type CliqueConfig struct {
   127  	Period uint64
   128  	Epoch  uint64
   129  }
   130  
   131  func DecodeConfig(cfg Provider) (c MagneticConfig, err error) {
   132  	m := cfg.GetStringMap("config")
   133  	err = mapstructure.WeakDecode(m, &c)
   134  	return
   135  }
   136  
   137  type Provider interface {
   138  	GetString(key string) string
   139  	GetInt(key string) int
   140  	GetBool(key string) bool
   141  	GetStringMap(key string) map[string]interface{}
   142  	GetStringMapString(key string) map[string]string
   143  	GetStringSlice(key string) []string
   144  	Get(key string) interface{}
   145  	Set(key string, value interface{})
   146  	IsSet(key string) bool
   147  	WatchConfig()
   148  	OnConfigChange(run func(in fsnotify.Event))
   149  	Unmarshal(rawVal interface{}, opts ...viper.DecoderConfigOption) error
   150  }
   151  
   152  // FromConfigString creates a config from the given YAML, JSON or TOML config. This is useful in tests.
   153  func FromConfigString(path, configType string) (Provider, error) {
   154  	v := viper.New()
   155  	v.SetConfigType(configType)
   156  	v.SetConfigFile(path)
   157  	if err := v.ReadInConfig(); err != nil {
   158  		return nil, err
   159  	}
   160  	return v, nil
   161  }
   162  
   163  // GetStringSlicePreserveString returns a string slice from the given config and key.
   164  // It differs from the GetStringSlice method in that if the config value is a string,
   165  // we do not attempt to split it into fields.
   166  func GetStringSlicePreserveString(cfg Provider, key string) []string {
   167  	sd := cfg.Get(key)
   168  	if sds, ok := sd.(string); ok {
   169  		return []string{sds}
   170  	} else {
   171  		return cast.ToStringSlice(sd)
   172  	}
   173  }
   174  
   175  
   176  type Mongo struct {
   177  	Addr      string
   178  	Timeout   time.Duration
   179  	PoolLimit int
   180  	Database  string
   181  }