package config import ( "context" "encoding/json" "fmt" "os" "strings" "go.etcd.io/etcd/client" ) var Conf *Configure // mysql 配置 type MysqlConfig struct { User string `json:"user"` Password string `json:"password"` Addr string `json:"addr"` DefaultDB string `json:"default_db"` Charset string `json:"charset"` MaxIdle json.Number `json:"max_idle"` MaxConn json.Number `json:"max_conn"` } // redis 配置 type RedisConfig struct { Network string `json:"network"` Address string `json:"addr"` Password string `json:"password"` DefaultDB string `json:"default_db"` ConnectTimeout json.Number `json:"connect_timeout"` ReadTimeout json.Number `json:"read_timeout"` WriteTimeout json.Number `json:"write_timeout"` IdleTimeout json.Number `json:"idle_timeout"` MaxActive json.Number `json:"max_active"` MaxIdle json.Number `json:"mac_idle"` Wait json.Number `json:"wait_flag"` } type ElasticConfig struct { Addr string `json:"addr"` Sniff string `json:"sniff"` } type LogConfig struct { MaxSize json.Number `json:"max_size"` MaxBackups json.Number `json:"max_backups"` MaxAge json.Number `json:"max_age"` DisableStacktrace string `json:"disable_stacktrace"` } type RPCNode struct { Scheme string `json:"scheme"` Name string `json:"name"` UpdateInterval json.Number `json:"update_interval"` MysqlDB string `json:"mysql_db"` RedisDB string `json:"redis_db"` Log LogConfig `json:"log"` } type RPCConfig struct { BasePath string `json:"base_path"` Vehicle RPCNode `json:"vehicle"` User RPCNode `json:"user"` } type Configure struct { RunMode string `json:"run_mode"` AppKey string `json:"app_key"` AppSecret string `json:"app_secret"` Mysql MysqlConfig `json:"mysql"` Redis RedisConfig `json:"redis"` Elastic ElasticConfig `json:"elastic"` Rpc RPCConfig `json:"rpc"` } // key 为加密密钥 func GetConfig(runmode, key string, cli client.Client) *Configure { keysAPI := client.NewKeysAPI(cli) basePath := fmt.Sprintf("/%s/config", runmode) if resp, err := keysAPI.Get(context.Background(), basePath, &client.GetOptions{ Recursive: true, }); err == nil && resp != nil && resp.Node != nil { Conf = &Configure{} value := getNodeData(key, resp.Node) if jsonStr, err := json.Marshal(value); err == nil { if err := json.Unmarshal(jsonStr, &Conf); err == nil { return Conf } else { fmt.Printf("json Unmarshal failed. error:%s", err) } } else { fmt.Printf("json Marshal failed. error:%s", err) } } else { fmt.Printf("get %s failed. error:%s", basePath, err) os.Exit(1) } return nil } // 递归取出node的叶子节点值 func getNodeData(key string, head *client.Node) (value interface{}) { s0 := strings.Split(head.Key, "/") len0 := len(s0) if len0 == 0 { return } if head.Dir { mapData := map[string]interface{}{} for _, node := range head.Nodes { s1 := strings.Split(node.Key, "/") len1 := len(s1) if len1 == 0 { break } mapData[s1[len1-1]] = getNodeData(key, node) } value = mapData } else { if key != "" && head.Value != "" { if bytesData, err := Base64URLDecode(head.Value); err != nil { fmt.Printf("Base64URLDecode(%s) failed. error:%s", head.Value, err) os.Exit(1) } else { if data, err := AesDecrypt(bytesData, []byte(key)); err != nil { fmt.Printf("AesDecrypt failed. error:%s", err) os.Exit(1) } else { value = string(data) } } } else { // 无加密,直接取值 value = head.Value } } return }