| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- // 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)
- }
|