config.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package config
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "os"
  7. "strings"
  8. "go.etcd.io/etcd/client"
  9. )
  10. var Conf *Configure
  11. // mysql 配置
  12. type MysqlConfig struct {
  13. User string `json:"user"`
  14. Password string `json:"password"`
  15. Addr string `json:"addr"`
  16. DefaultDB string `json:"default_db"`
  17. Charset string `json:"charset"`
  18. MaxIdle json.Number `json:"max_idle"`
  19. MaxConn json.Number `json:"max_conn"`
  20. }
  21. // redis 配置
  22. type RedisConfig struct {
  23. Network string `json:"network"`
  24. Address string `json:"addr"`
  25. Password string `json:"password"`
  26. DefaultDB string `json:"default_db"`
  27. ConnectTimeout json.Number `json:"connect_timeout"`
  28. ReadTimeout json.Number `json:"read_timeout"`
  29. WriteTimeout json.Number `json:"write_timeout"`
  30. IdleTimeout json.Number `json:"idle_timeout"`
  31. MaxActive json.Number `json:"max_active"`
  32. MaxIdle json.Number `json:"mac_idle"`
  33. Wait json.Number `json:"wait_flag"`
  34. }
  35. type ElasticConfig struct {
  36. Addr string `json:"addr"`
  37. Sniff string `json:"sniff"`
  38. }
  39. type LogConfig struct {
  40. MaxSize json.Number `json:"max_size"`
  41. MaxBackups json.Number `json:"max_backups"`
  42. MaxAge json.Number `json:"max_age"`
  43. DisableStacktrace string `json:"disable_stacktrace"`
  44. }
  45. type RPCNode struct {
  46. Scheme string `json:"scheme"`
  47. Name string `json:"name"`
  48. UpdateInterval json.Number `json:"update_interval"`
  49. MysqlDB string `json:"mysql_db"`
  50. RedisDB string `json:"redis_db"`
  51. Log LogConfig `json:"log"`
  52. }
  53. type RPCConfig struct {
  54. BasePath string `json:"base_path"`
  55. Vehicle RPCNode `json:"vehicle"`
  56. User RPCNode `json:"user"`
  57. }
  58. type Configure struct {
  59. RunMode string `json:"run_mode"`
  60. AppKey string `json:"app_key"`
  61. AppSecret string `json:"app_secret"`
  62. Mysql MysqlConfig `json:"mysql"`
  63. Redis RedisConfig `json:"redis"`
  64. Elastic ElasticConfig `json:"elastic"`
  65. Rpc RPCConfig `json:"rpc"`
  66. }
  67. // key 为加密密钥
  68. func GetConfig(runmode, key string, cli client.Client) *Configure {
  69. keysAPI := client.NewKeysAPI(cli)
  70. basePath := fmt.Sprintf("/%s/config", runmode)
  71. if resp, err := keysAPI.Get(context.Background(), basePath, &client.GetOptions{
  72. Recursive: true,
  73. }); err == nil && resp != nil && resp.Node != nil {
  74. Conf = &Configure{}
  75. value := getNodeData(key, resp.Node)
  76. if jsonStr, err := json.Marshal(value); err == nil {
  77. if err := json.Unmarshal(jsonStr, &Conf); err == nil {
  78. return Conf
  79. } else {
  80. fmt.Printf("json Unmarshal failed. error:%s", err)
  81. }
  82. } else {
  83. fmt.Printf("json Marshal failed. error:%s", err)
  84. }
  85. } else {
  86. fmt.Printf("get %s failed. error:%s", basePath, err)
  87. os.Exit(1)
  88. }
  89. return nil
  90. }
  91. // 递归取出node的叶子节点值
  92. func getNodeData(key string, head *client.Node) (value interface{}) {
  93. s0 := strings.Split(head.Key, "/")
  94. len0 := len(s0)
  95. if len0 == 0 {
  96. return
  97. }
  98. if head.Dir {
  99. mapData := map[string]interface{}{}
  100. for _, node := range head.Nodes {
  101. s1 := strings.Split(node.Key, "/")
  102. len1 := len(s1)
  103. if len1 == 0 {
  104. break
  105. }
  106. mapData[s1[len1-1]] = getNodeData(key, node)
  107. }
  108. value = mapData
  109. } else {
  110. if key != "" && head.Value != "" {
  111. if bytesData, err := Base64URLDecode(head.Value); err != nil {
  112. fmt.Printf("Base64URLDecode(%s) failed. error:%s", head.Value, err)
  113. os.Exit(1)
  114. } else {
  115. if data, err := AesDecrypt(bytesData, []byte(key)); err != nil {
  116. fmt.Printf("AesDecrypt failed. error:%s", err)
  117. os.Exit(1)
  118. } else {
  119. value = string(data)
  120. }
  121. }
  122. } else {
  123. // 无加密,直接取值
  124. value = head.Value
  125. }
  126. }
  127. return
  128. }