config.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright (c) 2015 qianqiusoft.com
  2. // Licensed to You under the GNU Affero GPL v3
  3. // See the LICENSE file at git.qianqiusoft.com/qianqiusoft/light-vocation/LICENSE
  4. // http://www.gnu.org/licenses/why-affero-gpl.en.html
  5. // config.go
  6. package utils
  7. import (
  8. "errors"
  9. "fmt"
  10. "os"
  11. "strconv"
  12. "strings"
  13. "github.com/astaxie/beego"
  14. )
  15. type ConfigHelper struct {
  16. }
  17. func NewConfig() *ConfigHelper {
  18. return &ConfigHelper{}
  19. }
  20. // get string value of the config by the key
  21. // the value is from env or app.conf
  22. func (this *ConfigHelper) String(key string) string {
  23. key = strings.TrimSpace(key)
  24. if key == "" {
  25. beego.Error("ConfigHelper.String : param key is empty")
  26. return ""
  27. }
  28. val := os.Getenv(key)
  29. val = strings.TrimSpace(val)
  30. if val == "" {
  31. val = beego.AppConfig.String(key)
  32. val = strings.TrimSpace(val)
  33. }
  34. return val
  35. }
  36. // get int value of config by key
  37. // the value is from env or app.conf
  38. // param key : key
  39. // param defaultValue : default value
  40. func (this *ConfigHelper) Int64(key string, defaultValue int64) (int64, error) {
  41. key = strings.TrimSpace(key)
  42. if key == "" {
  43. beego.Error("ConfigHelper.Int64 : param key is empty")
  44. return 0, errors.New("param ke is empty")
  45. }
  46. val := os.Getenv(key)
  47. val = strings.TrimSpace(val)
  48. if val == "" {
  49. val = beego.AppConfig.String(key)
  50. val = strings.TrimSpace(val)
  51. beego.Debug(fmt.Sprintf("ConfigHelper.Int64 value of key %s is %s from app.conf", key, val))
  52. } else {
  53. beego.Debug(fmt.Sprintf("ConfigHelper.Int64 value of key %s is %s from env", key, val))
  54. }
  55. if val == "" {
  56. beego.Debug("ConfigHelper.Bool return default value ")
  57. return defaultValue, nil
  58. }
  59. return strconv.ParseInt(val, 10, 64)
  60. }
  61. // get bool value of config by key
  62. // the value is from env or app.conf
  63. // param key : key
  64. // param defaultValue : default value
  65. func (this *ConfigHelper) Bool(key string, defaultValue bool) (bool, error) {
  66. key = strings.TrimSpace(key)
  67. if key == "" {
  68. beego.Error("ConfigHelper.Bool : param key is empty")
  69. return false, errors.New("param ke is empty")
  70. }
  71. val := os.Getenv(key)
  72. val = strings.TrimSpace(val)
  73. if val == "" {
  74. val = beego.AppConfig.String(key)
  75. val = strings.TrimSpace(val)
  76. beego.Debug(fmt.Sprintf("ConfigHelper.Bool value of key %s is %s from app.conf", key, val))
  77. } else {
  78. beego.Debug(fmt.Sprintf("ConfigHelper.Bool value of key %s is %s from env", key, val))
  79. }
  80. if val == "" {
  81. beego.Debug("ConfigHelper.Bool return default value ")
  82. return defaultValue, nil
  83. }
  84. return strconv.ParseBool(val)
  85. }
  86. // get the string value from env
  87. // param key : key
  88. // return : it will be "" if there is not key
  89. func (this *ConfigHelper) StringEnv(key string) string {
  90. key = strings.TrimSpace(key)
  91. if key == "" {
  92. beego.Error("utConfigHelperils.StringEnv : param key is empty")
  93. return ""
  94. }
  95. val := os.Getenv(key)
  96. val = strings.TrimSpace(val)
  97. return val
  98. }
  99. // get the int64 value from env
  100. // param key : key
  101. // return : value and error
  102. func (this *ConfigHelper) Int64Env(key string) (int64, error) {
  103. key = strings.TrimSpace(key)
  104. if key == "" {
  105. beego.Error("ConfigHelper.Int64Env : param key is empty")
  106. return 0, errors.New("param ke is empty")
  107. }
  108. val := os.Getenv(key)
  109. val = strings.TrimSpace(val)
  110. if val == "" {
  111. beego.Debug("ConfigHelper.Bool return default value ")
  112. return 0, errors.New("value of key is not exists")
  113. }
  114. return strconv.ParseInt(val, 10, 64)
  115. }
  116. // get bool value of config by key from env
  117. // param key : key
  118. // return : value and error
  119. func (this *ConfigHelper) BoolEnv(key string, defaultValue bool) (bool, error) {
  120. key = strings.TrimSpace(key)
  121. if key == "" {
  122. beego.Error("ConfigHelper.Bool : param key is empty")
  123. return false, errors.New("param ke is empty")
  124. }
  125. val := os.Getenv(key)
  126. val = strings.TrimSpace(val)
  127. if val == "" {
  128. beego.Debug("ConfigHelper.Bool return default value ")
  129. return false, errors.New("value of key is not exists")
  130. }
  131. return strconv.ParseBool(val)
  132. }