config.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package config
  2. import (
  3. "crypto/md5"
  4. "encoding/json"
  5. "fmt"
  6. "os"
  7. "reflect"
  8. "strings"
  9. "sync"
  10. "github.com/yuguorong/go/log"
  11. )
  12. type Configs struct {
  13. fileds map[string]interface{}
  14. mutex sync.Locker
  15. }
  16. func (c *Configs) LoadConfig() {
  17. c.LoadFromFS()
  18. }
  19. func (c *Configs) Password(passwd string) string {
  20. data := []byte(passwd)
  21. has := md5.Sum(data)
  22. md5str1 := fmt.Sprintf("%x", has) //将[]byte转成16进制
  23. //log.Info(md5str1)
  24. return md5str1
  25. }
  26. func (c *Configs) genUUID() {
  27. f, _ := os.OpenFile("/dev/urandom", os.O_RDONLY, 0)
  28. b := make([]byte, 16)
  29. f.Read(b)
  30. f.Close()
  31. uuid := fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
  32. log.Info(uuid)
  33. }
  34. //Load Get system debug configuration
  35. func (c *Configs) LoadFromFS() {
  36. if file, err := os.Open("conf.json"); err == nil {
  37. defer file.Close()
  38. var tmp = make([]byte, 1024)
  39. n, err := file.Read(tmp)
  40. if err == nil {
  41. confv := make(map[string]interface{})
  42. err = json.Unmarshal(tmp[:n], &confv)
  43. if err == nil {
  44. c.fileds = confv
  45. } else {
  46. log.Error(err)
  47. }
  48. }
  49. }
  50. }
  51. //Update Save sys config
  52. func (c *Configs) Save() {
  53. f, _ := os.OpenFile("conf.json", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
  54. defer f.Close()
  55. jsonStu, _ := json.Marshal(c.fileds)
  56. f.WriteString(string(jsonStu))
  57. log.Info("SysConfig saved!")
  58. }
  59. func (c *Configs) LoadFromCloud() {
  60. }
  61. func (c *Configs) GetValue(fieldPath string, defVal interface{}) interface{} {
  62. spath := strings.Split(fieldPath, "/")
  63. isSimpleT := true
  64. if defVal != nil {
  65. vt := reflect.TypeOf(defVal)
  66. if vt.Kind() >= reflect.Array && vt.Kind() != reflect.String {
  67. isSimpleT = false
  68. }
  69. }
  70. lens := len(spath)
  71. if lens > 0 {
  72. lpath := c.fileds
  73. c.mutex.Lock()
  74. for i := 0; i < lens-1; i++ {
  75. gwc, has := (lpath)[spath[i]]
  76. if !has {
  77. newp := make(map[string]interface{})
  78. lpath[spath[i]] = newp
  79. lpath = newp
  80. } else {
  81. lpath = gwc.(map[string]interface{})
  82. }
  83. }
  84. c.mutex.Unlock()
  85. value, has := lpath[spath[lens-1]]
  86. if has {
  87. if isSimpleT {
  88. return value
  89. } else {
  90. json.Unmarshal([]byte(value.(string)), defVal)
  91. return defVal
  92. }
  93. }
  94. }
  95. if isSimpleT {
  96. return defVal
  97. } else {
  98. return nil
  99. }
  100. }
  101. func (c *Configs) SetValue(fieldPath string, value interface{}) bool {
  102. spath := strings.Split(fieldPath, "/")
  103. lens := len(spath)
  104. if lens > 0 {
  105. c.mutex.Lock()
  106. defer c.mutex.Unlock()
  107. lpath := &c.fileds
  108. for i := 0; i < lens-1; i++ {
  109. gwc, has := (*lpath)[spath[i]]
  110. if !has {
  111. newp := make(map[string]interface{})
  112. (*lpath)[spath[i]] = newp
  113. lpath = &newp
  114. } else {
  115. v := gwc.(map[string]interface{})
  116. lpath = &v
  117. }
  118. }
  119. vt := reflect.TypeOf(value)
  120. if vt.Kind() < reflect.Array || vt.Kind() == reflect.String {
  121. if v, ok := (*lpath)[spath[lens-1]]; ok {
  122. if reflect.TypeOf(value) == reflect.TypeOf(v) && reflect.DeepEqual(v, value) {
  123. return false
  124. }
  125. }
  126. (*lpath)[spath[lens-1]] = value
  127. } else {
  128. sval, _ := json.Marshal(value)
  129. if v, ok := (*lpath)[spath[lens-1]]; ok {
  130. if v.(string) == string(sval) {
  131. return false
  132. }
  133. }
  134. (*lpath)[spath[lens-1]] = string(sval)
  135. }
  136. }
  137. return true
  138. }
  139. func (c *Configs) SetProfile(name string, prof interface{}) bool {
  140. path := "profile/" + name
  141. if c.SetValue(path, prof) {
  142. c.Save()
  143. return true
  144. }
  145. return false
  146. }
  147. func (c *Configs) GetProfile(name string, defV interface{}) interface{} {
  148. path := "profile/" + name
  149. prof := c.GetValue(path, defV)
  150. if prof == nil {
  151. prof = defV
  152. c.SetValue(path, defV)
  153. c.Save()
  154. }
  155. return prof
  156. }
  157. var sysConfig = &Configs{
  158. fileds: make(map[string]interface{}),
  159. mutex: &sync.Mutex{},
  160. }
  161. func GetSysConfig() *Configs {
  162. return sysConfig
  163. }
  164. func init() {
  165. sysConfig.LoadConfig()
  166. }