// Copyright (c) 2015 qianqiusoft.com // Licensed to You under the GNU Affero GPL v3 // See the LICENSE file at git.qianqiusoft.com/qianqiusoft/light-vocation/LICENSE // http://www.gnu.org/licenses/why-affero-gpl.en.html // config.go package utils import ( "errors" "fmt" "os" "strconv" "strings" "github.com/astaxie/beego" ) type ConfigHelper struct { } func NewConfig() *ConfigHelper { return &ConfigHelper{} } // get string value of the config by the key // the value is from env or app.conf func (this *ConfigHelper) String(key string) string { key = strings.TrimSpace(key) if key == "" { beego.Error("ConfigHelper.String : param key is empty") return "" } val := os.Getenv(key) val = strings.TrimSpace(val) if val == "" { val = beego.AppConfig.String(key) val = strings.TrimSpace(val) } return val } // get int value of config by key // the value is from env or app.conf // param key : key // param defaultValue : default value func (this *ConfigHelper) Int64(key string, defaultValue int64) (int64, error) { key = strings.TrimSpace(key) if key == "" { beego.Error("ConfigHelper.Int64 : param key is empty") return 0, errors.New("param ke is empty") } val := os.Getenv(key) val = strings.TrimSpace(val) if val == "" { val = beego.AppConfig.String(key) val = strings.TrimSpace(val) beego.Debug(fmt.Sprintf("ConfigHelper.Int64 value of key %s is %s from app.conf", key, val)) } else { beego.Debug(fmt.Sprintf("ConfigHelper.Int64 value of key %s is %s from env", key, val)) } if val == "" { beego.Debug("ConfigHelper.Bool return default value ") return defaultValue, nil } return strconv.ParseInt(val, 10, 64) } // get bool value of config by key // the value is from env or app.conf // param key : key // param defaultValue : default value func (this *ConfigHelper) Bool(key string, defaultValue bool) (bool, error) { key = strings.TrimSpace(key) if key == "" { beego.Error("ConfigHelper.Bool : param key is empty") return false, errors.New("param ke is empty") } val := os.Getenv(key) val = strings.TrimSpace(val) if val == "" { val = beego.AppConfig.String(key) val = strings.TrimSpace(val) beego.Debug(fmt.Sprintf("ConfigHelper.Bool value of key %s is %s from app.conf", key, val)) } else { beego.Debug(fmt.Sprintf("ConfigHelper.Bool value of key %s is %s from env", key, val)) } if val == "" { beego.Debug("ConfigHelper.Bool return default value ") return defaultValue, nil } return strconv.ParseBool(val) } // get the string value from env // param key : key // return : it will be "" if there is not key func (this *ConfigHelper) StringEnv(key string) string { key = strings.TrimSpace(key) if key == "" { beego.Error("utConfigHelperils.StringEnv : param key is empty") return "" } val := os.Getenv(key) val = strings.TrimSpace(val) return val } // get the int64 value from env // param key : key // return : value and error func (this *ConfigHelper) Int64Env(key string) (int64, error) { key = strings.TrimSpace(key) if key == "" { beego.Error("ConfigHelper.Int64Env : param key is empty") return 0, errors.New("param ke is empty") } val := os.Getenv(key) val = strings.TrimSpace(val) if val == "" { beego.Debug("ConfigHelper.Bool return default value ") return 0, errors.New("value of key is not exists") } return strconv.ParseInt(val, 10, 64) } // get bool value of config by key from env // param key : key // return : value and error func (this *ConfigHelper) BoolEnv(key string, defaultValue bool) (bool, error) { key = strings.TrimSpace(key) if key == "" { beego.Error("ConfigHelper.Bool : param key is empty") return false, errors.New("param ke is empty") } val := os.Getenv(key) val = strings.TrimSpace(val) if val == "" { beego.Debug("ConfigHelper.Bool return default value ") return false, errors.New("value of key is not exists") } return strconv.ParseBool(val) }