|
| 1 | +package conf |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "github.com/getCompassUtils/go_base_frame/api/system/flags" |
| 7 | + "os" |
| 8 | + "path" |
| 9 | + "runtime" |
| 10 | + "sync/atomic" |
| 11 | +) |
| 12 | + |
| 13 | +// ShardingStruct структура файла sharding.json |
| 14 | +type ShardingStruct struct { |
| 15 | + Mysql map[string]MysqlShardingStruct `json:"mysql,omitempty"` |
| 16 | + Rabbit map[string]RabbitShardingStruct `json:"rabbit,omitempty"` |
| 17 | + Go map[string]GoShardingStruct `json:"go,omitempty"` |
| 18 | +} |
| 19 | + |
| 20 | +// MysqlShardingStruct структура mysql шардом |
| 21 | +type MysqlShardingStruct struct { |
| 22 | + Db string `json:"db"` |
| 23 | + Mysql struct { |
| 24 | + Host string `json:"host"` |
| 25 | + User string `json:"user"` |
| 26 | + Pass string `json:"pass"` |
| 27 | + Ssl bool `json:"ssl"` |
| 28 | + MaxConnections int `json:"max_connections"` |
| 29 | + } `json:"mysql"` |
| 30 | + Schemas map[string]string `json:"schemas"` |
| 31 | +} |
| 32 | + |
| 33 | +// RabbitShardingStruct структура rabbit шардов |
| 34 | +type RabbitShardingStruct struct { |
| 35 | + Host string `json:"host"` |
| 36 | + Port string `json:"port"` |
| 37 | + User string `json:"user"` |
| 38 | + Pass string `json:"pass"` |
| 39 | +} |
| 40 | + |
| 41 | +// GoShardingStruct структура go шардов |
| 42 | +type GoShardingStruct struct { |
| 43 | + Host string `json:"host,omitempty"` |
| 44 | + Port string `json:"port,omitempty"` |
| 45 | + Nodes []GoNodeShardingStruct `json:"nodes,omitempty"` |
| 46 | +} |
| 47 | + |
| 48 | +// GoNodeShardingStruct структура go ноды |
| 49 | +type GoNodeShardingStruct struct { |
| 50 | + Id int64 `json:"id"` |
| 51 | + Host string `json:"host"` |
| 52 | + Port string `json:"port"` |
| 53 | +} |
| 54 | + |
| 55 | +// ------------------------------------------------------- |
| 56 | +// пакет получения sharding конфигурации |
| 57 | +// ------------------------------------------------------- |
| 58 | + |
| 59 | +// переменная содержащая конфигурацию |
| 60 | +var shardingConfig atomic.Value |
| 61 | + |
| 62 | +// ------------------------------------------------------- |
| 63 | +// PUBLIC |
| 64 | +// ------------------------------------------------------- |
| 65 | + |
| 66 | +// UpdateShardingConfig обновляем конфигурацию |
| 67 | +func UpdateShardingConfig() error { |
| 68 | + |
| 69 | + tempPath := flags.ConfDir |
| 70 | + if tempPath == "" { |
| 71 | + |
| 72 | + _, b, _, _ := runtime.Caller(0) |
| 73 | + tempPath = path.Join(path.Dir(b)) // nosemgrep |
| 74 | + } |
| 75 | + |
| 76 | + // сохраняем конфигурацию |
| 77 | + decodedInfo, err := getShardingConfigFromFile(tempPath + "/sharding.json") |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + shardingConfig.Store(decodedInfo) |
| 83 | + |
| 84 | + return nil |
| 85 | +} |
| 86 | + |
| 87 | +// получаем конфиг из файла |
| 88 | +func getShardingConfigFromFile(path string) (ShardingStruct, error) { |
| 89 | + |
| 90 | + // открываем файл с конфигурацией |
| 91 | + file, err := os.Open(path) |
| 92 | + if err != nil { |
| 93 | + return ShardingStruct{}, fmt.Errorf("unable read file sharding.json, error: %v", err) |
| 94 | + } |
| 95 | + |
| 96 | + // считываем информацию из файла в переменную |
| 97 | + decoder := json.NewDecoder(file) |
| 98 | + var decodedInfo ShardingStruct |
| 99 | + err = decoder.Decode(&decodedInfo) |
| 100 | + if err != nil { |
| 101 | + return ShardingStruct{}, fmt.Errorf("unable decode file sharding.json, error: %v", err) |
| 102 | + } |
| 103 | + |
| 104 | + // закрываем файл |
| 105 | + _ = file.Close() |
| 106 | + |
| 107 | + return decodedInfo, nil |
| 108 | +} |
| 109 | + |
| 110 | +// GetShardingConfig получаем конфигурацию custom |
| 111 | +func GetShardingConfig() ShardingStruct { |
| 112 | + |
| 113 | + config := shardingConfig.Load() |
| 114 | + if config == nil { |
| 115 | + |
| 116 | + err := UpdateShardingConfig() |
| 117 | + if err != nil { |
| 118 | + panic(err) |
| 119 | + } |
| 120 | + |
| 121 | + config = shardingConfig.Load() |
| 122 | + } |
| 123 | + |
| 124 | + return config.(ShardingStruct) |
| 125 | +} |
0 commit comments