1
0
SongZihuan ce3e177cad feat: 经过若干适配 1 жил өмнө
..
config.go ce3e177cad feat: 经过若干适配 1 жил өмнө
config_test.go ce3e177cad feat: 经过若干适配 1 жил өмнө
options.go 425be6b4a1 fix golint issues in core/conf (#476) 4 жил өмнө
properties.go ce3e177cad feat: 经过若干适配 1 жил өмнө
properties_test.go ce3e177cad feat: 经过若干适配 1 жил өмнө
readme.md ab9eeff500 fix: problem on name overlaping in config (#2820) 2 жил өмнө

readme.md

How to use

  1. Define a config structure, like below:

    type RestfulConf struct {
    ServiceName  string        `json:",env=SERVICE_NAME"`  // read from env automatically
    	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())