Kevin Wan 3da5c5f530 Update readme.md hace 2 años
..
config.go 1e717f9f5c feat: add toml config (#1899) hace 3 años
config_test.go 1e717f9f5c feat: add toml config (#1899) hace 3 años
options.go 425be6b4a1 fix golint issues in core/conf (#476) hace 4 años
properties.go a91c3907a8 feat: rename module from tal-tech to zeromicro (#1413) hace 3 años
properties_test.go a91c3907a8 feat: rename module from tal-tech to zeromicro (#1413) hace 3 años
readme.md 3da5c5f530 Update readme.md hace 2 años

readme.md

How to use

  1. Define a config structure, like below:

    type RestfulConf struct {
    	Host         string        `json:",default=0.0.0.0"`
    	Port         int
    	LogMode      string        `json:",options=[file,console]"`
    	Verbose      bool          `json:",optional"`
    	MaxConns     int           `json:",default=10000"`
    	MaxBytes     int64         `json:",default=1048576"`
    	Timeout      time.Duration `json:",default=3s"`
    	CpuThreshold int64         `json:",default=900,range=[0:1000]"`
    }
    
  2. Write the yaml, toml or json config file:

  • yaml example

    # most fields are optional or have default values
    Port: 8080
    LogMode: console
    # you can use env settings
    MaxBytes: ${MAX_BYTES}
    
  • toml example

    # most fields are optional or have default values
    Port = 8_080
    LogMode = "console"
    # you can use env settings
    MaxBytes = "${MAX_BYTES}"
    
  1. Load the config from a file:

    // exit on error
    var config RestfulConf
    conf.MustLoad(configFile, &config)
    
    // or handle the error on your own
    var config RestfulConf
    if err := conf.Load(configFile, &config); err != nil {
    log.Fatal(err)
    }
    
    // enable reading from environments
    var config RestfulConf
    conf.MustLoad(configFile, &config, conf.UseEnv())