-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathclient.go
146 lines (132 loc) · 5.09 KB
/
client.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
package request
import (
"log"
"net/http"
"strconv"
"com.lc.go.codepush/server/config"
"com.lc.go.codepush/server/db/redis"
"com.lc.go.codepush/server/model"
"com.lc.go.codepush/server/model/constants"
"com.lc.go.codepush/server/utils"
"github.com/gin-gonic/gin"
)
type Client struct{}
type updateInfo struct {
DownloadUrl string `json:"download_url"`
Description string `json:"description"`
IsAvailable bool `json:"is_available"`
IsDisabled bool `json:"is_disabled"`
TargetBinaryRange string `json:"target_binary_range"`
PackageHash string `json:"package_hash"`
Label string `json:"label"`
PackageSize int64 `json:"package_size"`
UpdateAppVersion bool `json:"update_app_version"`
ShouldRunBinaryVersion bool `json:"should_run_binary_version"`
IsMandatory bool `json:"is_mandatory"`
}
type updateInfoRedisInfo struct {
updateInfo
NewVersion string
}
func (Client) CheckUpdate(ctx *gin.Context) {
deploymentKey := ctx.Query("deployment_key")
appVersion := ctx.Query("app_version")
packageHash := ctx.Query("package_hash")
// label := ctx.Query("label")
// clientUniqueId := ctx.Query("client_unique_id")
redisKey := constants.REDIS_UPDATE_INFO + deploymentKey + ":" + appVersion
updateInfoRedis := redis.GetRedisObj[updateInfoRedisInfo](redisKey)
updateInfo := updateInfo{}
config := config.GetConfig()
if updateInfoRedis == nil {
updateInfoRedis = &updateInfoRedisInfo{}
deployment := model.GetOne[model.Deployment]("key", deploymentKey)
if deployment == nil {
log.Panic("Key error")
}
deploymentVersion := model.DeploymentVersion{}.GetByKeyDeploymentIdAndVersion(*deployment.Id, appVersion)
if deploymentVersion != nil {
if deploymentVersion.CurrentPackage != nil {
packag := model.GetOne[model.Package]("id", deploymentVersion.CurrentPackage)
if packag != nil {
// && *packag.Hash != packageHash
updateInfoRedis.TargetBinaryRange = *deploymentVersion.AppVersion
updateInfoRedis.PackageHash = *packag.Hash
updateInfoRedis.PackageSize = *packag.Size
updateInfoRedis.IsAvailable = true
updateInfoRedis.IsMandatory = true
label := strconv.Itoa(*packag.Id)
updateInfoRedis.Label = label
updateInfoRedis.DownloadUrl = config.ResourceUrl + *packag.Download
if packag.Description != nil {
updateInfoRedis.Description = *packag.Description
}
}
}
}
deploymentVersionNew := model.DeploymentVersion{}.GetNewVersionByKeyDeploymentId(*deployment.Id)
if deploymentVersionNew != nil {
updateInfoRedis.NewVersion = *deploymentVersionNew.AppVersion
}
redis.SetRedisObj(redisKey, updateInfoRedis, -1)
}
if updateInfoRedis.PackageHash != "" {
if updateInfoRedis.PackageHash != packageHash && appVersion == updateInfoRedis.TargetBinaryRange {
updateInfo.TargetBinaryRange = updateInfoRedis.TargetBinaryRange
updateInfo.PackageHash = updateInfoRedis.PackageHash
updateInfo.PackageSize = updateInfoRedis.PackageSize
updateInfo.IsAvailable = true
updateInfo.IsMandatory = true
updateInfo.Label = updateInfoRedis.Label
updateInfo.DownloadUrl = updateInfoRedis.DownloadUrl
updateInfo.Description = updateInfoRedis.Description
} else if updateInfoRedis.NewVersion != "" && appVersion != updateInfoRedis.NewVersion && utils.FormatVersionStr(appVersion) < utils.FormatVersionStr(updateInfoRedis.NewVersion) {
updateInfo.TargetBinaryRange = updateInfoRedis.NewVersion
updateInfo.UpdateAppVersion = true
}
} else if updateInfoRedis.NewVersion != "" && appVersion != updateInfoRedis.NewVersion && utils.FormatVersionStr(appVersion) < utils.FormatVersionStr(updateInfoRedis.NewVersion) {
updateInfo.TargetBinaryRange = updateInfoRedis.NewVersion
updateInfo.UpdateAppVersion = true
}
ctx.JSON(http.StatusOK, gin.H{
"update_info": updateInfo,
})
}
type reportStatuReq struct {
AppVersion *string `json:"app_version"`
DeploymentKey *string `json:"deployment_key"`
ClientUniqueId *string `json:"client_unique_id"`
Label *string `json:"label"`
Status *string `json:"status"`
PreviousLabelOrAppVersion *string `json:"previous_label_or_app_version"`
PreviousDeploymentKey *string `json:"previous_deployment_key"`
}
func (Client) ReportStatus(ctx *gin.Context) {
json := reportStatuReq{}
ctx.BindJSON(&json)
if json.Status != nil {
pack := model.GetOne[model.Package]("id=?", json.Label)
if pack != nil {
if *json.Status == "DeploymentSucceeded" {
model.Package{}.AddActive(*pack.Id)
} else if *json.Status == "DeploymentFailed" {
model.Package{}.AddFailed(*pack.Id)
}
}
}
ctx.String(http.StatusOK, "OK")
}
type downloadReq struct {
ClientUniqueId *string `json:"client_unique_id"`
DeploymentKey *string `json:"deployment_key"`
Label *string `json:"label"`
}
func (Client) Download(ctx *gin.Context) {
json := downloadReq{}
ctx.BindJSON(&json)
pack := model.GetOne[model.Package]("id=?", json.Label)
if pack != nil {
model.Package{}.AddInstalled(*pack.Id)
}
ctx.String(http.StatusOK, "OK")
}