/
checkVersion.go
215 lines (181 loc) · 5.67 KB
/
checkVersion.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package task
import (
"encoding/json"
"github.com/goflyfox/gcsc/client"
"github.com/goflyfox/gcsc/constant"
"github.com/goflyfox/gcsc/utils/resp"
"github.com/gogf/gf/crypto/gmd5"
"github.com/gogf/gf/encoding/gjson"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
"github.com/gogf/gf/os/gfile"
"github.com/gogf/gf/os/glog"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/text/gstr"
)
func CheckVersion() {
// form := base.BaseForm{}
// config.TbConfigPublic{}.GetCacheModel(&form)
projectNameStr := g.Config().GetString(constant.ParamProjectName)
projectSecretStr := g.Config().GetString(constant.ParamProjectSecret)
if projectNameStr == "" {
glog.Warning("[ConfigClient]updateData projects is null")
return
}
projectNames := gstr.Split(projectNameStr, ",")
projectSecrets := gstr.Split(projectSecretStr, ",")
if len(projectNames) != len(projectSecrets) {
glog.Error("[ConfigClient]projects config length err")
return
}
serverUrl := g.Config().GetString(constant.ParamServerUrl, "http://127.0.0.1")
serverVersionPath := g.Config().GetString(constant.ParamServerVersionPath, "/config/api/version")
serverDataPath := g.Config().GetString(constant.ParamServerDataPath, "/config/api/data")
for index, projectName := range projectNames {
projectSecret := projectSecrets[index]
nowTime := gtime.Now().Format("YmdHis")
mac, err := gmd5.Encrypt(projectName + nowTime + projectSecret)
if err != nil {
glog.Error("[ConfigClient]updateData md5 err", err)
continue
}
// 查询版本
var serverVersion string
reqUrl := serverUrl + serverVersionPath + "?name=" + projectName + "&no=" + nowTime + "&mac=" + mac
if r, e := ghttp.Get(reqUrl); e != nil {
glog.Error("[ConfigClient]reqUrl error:"+reqUrl, err)
} else {
defer r.Close()
content := string(r.ReadAll())
var respData resp.Resp
err := json.Unmarshal([]byte(content), &respData)
if err != nil {
glog.Error("[ConfigClient]reqUrl resp to object error:"+reqUrl, err)
continue
}
if !respData.Success() {
glog.Error("[ConfigClient]projects config length error:" + reqUrl)
continue
}
serverVersion = respData.GetString("version")
}
cacheVersion := client.GetCache(projectName).Version
// 版本相同不需要处理
if cacheVersion == serverVersion {
continue
}
// 更新数据
listBean := new(client.ConfigListBean)
nowTime = gtime.Now().Format("YmdHis")
mac, err = gmd5.Encrypt(projectName + nowTime + projectSecret)
if err != nil {
glog.Error("[ConfigClient]updateData md5 err", err)
continue
}
reqUrl = serverUrl + serverDataPath + "?name=" + projectName + "&no=" + nowTime + "&mac=" + mac
if r, e := ghttp.Get(reqUrl); e != nil {
glog.Error("[ConfigClient]reqUrl error:"+reqUrl, err)
} else {
defer r.Close()
content := string(r.ReadAll())
glog.Info("[ConfigClient]reqUrl success", reqUrl, content)
var respData resp.Resp
err := json.Unmarshal([]byte(content), &respData)
if err != nil {
glog.Error("[ConfigClient]reqUrl resp to object error:"+reqUrl, err)
continue
}
if !respData.Success() {
glog.Error("[ConfigClient]projects config length error:" + reqUrl)
continue
}
dataContent := respData.GetString("content")
dataVersion := respData.GetString("version")
var dataList []client.ConfigBean
err = gjson.DecodeTo(dataContent, &dataList)
if err != nil {
glog.Error("[ConfigClient]reqUrl resp to object error:"+reqUrl, err)
continue
}
listBean.Version = dataVersion
listBean.Name = projectName
listBean.ListData = dataList
// 存储文件
data := projectName + "\r\n" + dataVersion + "\r\n" + dataContent
dataFilePath := GetDataPath() + gfile.Separator + projectName + ".txt"
gfile.PutBytes(dataFilePath, []byte(data))
// 设置缓存
client.SetCache(*listBean)
glog.Info("[ConfigClient]load cache and file:" + dataFilePath)
}
}
}
// 缓存数据初始化
func InitConfigData() {
glog.Info("[ConfigClient]initConfigData start...")
dataPath := GetDataPath()
glog.Info("[ConfigClient]dataPath:" + dataPath)
// 获取文件列表
files, err := gfile.DirNames(dataPath)
if err != nil {
glog.Error("[ConfigClient]dataPath list file error", err)
return
}
// 如果第一次不存在,进行一次获取
if len(files) <= 0 {
CheckVersion()
}
// 再次获取文件列表,不存在打印异常
files, err = gfile.DirNames(dataPath)
if err != nil {
glog.Error("[ConfigClient]dataPath list file error", err)
return
}
if len(files) <= 0 {
glog.Error("[ConfigClient]dataPath not exist file")
return
}
for _, filename := range files {
filePath := dataPath + gfile.Separator + filename
if !gfile.IsFile(filePath) {
continue
}
content := gfile.GetContents(filePath)
if content == "" {
glog.Error("[ConfigClient]file content empty :" + filePath)
continue
}
glog.Info(content)
lines := gstr.Split(content, "\r\n")
if len(lines) != 3 {
glog.Error("[ConfigClient]file content error :" + filePath)
continue
}
var dataList []client.ConfigBean
err = gjson.DecodeTo(lines[2], &dataList)
if err != nil {
glog.Error("[ConfigClient]reqUrl resp to object error", err)
continue
}
listBean := new(client.ConfigListBean)
listBean.Name = lines[0]
listBean.Version = lines[1]
listBean.ListData = dataList
// 设置缓存
client.SetCache(*listBean)
}
}
// 获取数据目录
func GetDataPath() string {
dataPath := g.Config().GetString(constant.ParamDataPath)
if dataPath == "" {
dataPath = gfile.TempDir() + gfile.Separator + "configClient"
}
if !gfile.Exists(dataPath) {
err := gfile.Mkdir(dataPath)
if err != nil {
glog.Error("[ConfigClient]create config path error")
}
}
return dataPath
}