Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
htdcx committed May 24, 2024
2 parents 624c624 + c8426e3 commit 9d8410b
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 37 deletions.
2 changes: 2 additions & 0 deletions DBPacth/v1.0.1-v1.0.2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE `package`
ADD COLUMN `deployment_version_id` INT NULL AFTER `deployment_id`;
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,5 @@ chmod +x code-push-go
<string moduleConfig="true" name="CodePushServerUrl">${CODE_PUSH_SERVER_URL}</string>
```

## Developing
- [X] Delete app
- [X] Delete deployment
- [ ] Rollback bundel

## License
MIT License [Read](https://github.com/htdcx/code-push-server-go/blob/main/LICENSE)
1 change: 1 addition & 0 deletions code-push.sql
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ DROP TABLE IF EXISTS `package`;
CREATE TABLE `package` (
`id` int NOT NULL AUTO_INCREMENT,
`deployment_id` int DEFAULT NULL,
`deployment_version_id` int DEFAULT NULL,
`size` bigint DEFAULT NULL,
`hash` varchar(256) DEFAULT NULL,
`description` TEXT DEFAULT NULL,
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func main() {
authApi.POST("/lsDeployment", request.App{}.LsDeployment)
authApi.GET("/lsApp", request.App{}.LsApp)
authApi.POST("/uploadBundle", request.App{}.UploadBundle)
authApi.POST("/rollback", request.App{}.Rollback)
}

g.Run(configs.Port)
Expand Down
4 changes: 4 additions & 0 deletions model/deploymentVersion.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ func (DeploymentVersion) GetNewVersionByKeyDeploymentId(deploymentId int) *Deplo
}
return deploymentVersion
}

func (DeploymentVersion) UpdateCurrentPackage(id int, pid *int) {
userDb.Raw("update deployment_version set current_package=? where id=?", pid, id).Scan(&DeploymentVersion{})
}
30 changes: 20 additions & 10 deletions model/package.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package model

type Package struct {
Id *int `gorm:"primarykey;autoIncrement;size:32"`
DeploymentId *int `json:"deploymentId"`
Size *int64 `json:"size"`
Hash *string `json:"hash"`
Download *string `json:"download"`
Active *int `json:"active"`
Failed *int `json:"failed"`
Installed *int `json:"installed"`
CreateTime *int64 `json:"create_time"`
Description *string `json:"description"`
Id *int `gorm:"primarykey;autoIncrement;size:32"`
DeploymentId *int `json:"deploymentId"`
DeploymentVersionId *int `json:"deploymentVersionId"`
Size *int64 `json:"size"`
Hash *string `json:"hash"`
Download *string `json:"download"`
Active *int `json:"active"`
Failed *int `json:"failed"`
Installed *int `json:"installed"`
CreateTime *int64 `json:"create_time"`
Description *string `json:"description"`
}

func (Package) TableName() string {
Expand All @@ -28,3 +29,12 @@ func (Package) AddFailed(pid int) {
func (Package) AddInstalled(pid int) {
userDb.Raw("update package set installed=installed+1 where id=?", pid).Scan(&Package{})
}

func (Package) GetRollbackPack(deploymentId int, lastPakcId int, deploymentVersionId int) *Package {
var lastPackage *Package
err := userDb.Where("deployment_id=?", deploymentId).Where("id<?", lastPakcId).Where("deployment_version_id", deploymentVersionId).Order("id desc").First(&lastPackage).Error
if err != nil {
return nil
}
return lastPackage
}
85 changes: 76 additions & 9 deletions request/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,16 @@ func (App) CreateBundle(ctx *gin.Context) {
// uuid, _ := uuid.NewUUID()
// hash := uuid.String()
newPackage := model.Package{
DeploymentId: deployment.Id,
Size: createBundleReq.Size,
Hash: createBundleReq.Hash,
Download: createBundleReq.DownloadUrl,
Description: createBundleReq.Description,
Active: utils.CreateInt(0),
Installed: utils.CreateInt(0),
Failed: utils.CreateInt(0),
CreateTime: utils.GetTimeNow(),
DeploymentId: deployment.Id,
DeploymentVersionId: deploymentVersion.Id,
Size: createBundleReq.Size,
Hash: createBundleReq.Hash,
Download: createBundleReq.DownloadUrl,
Description: createBundleReq.Description,
Active: utils.CreateInt(0),
Installed: utils.CreateInt(0),
Failed: utils.CreateInt(0),
CreateTime: utils.GetTimeNow(),
}
model.Create[model.Package](&newPackage)
deploymentVersion.CurrentPackage = newPackage.Id
Expand Down Expand Up @@ -423,3 +424,69 @@ func (App) DelDeployment(ctx *gin.Context) {
log.Panic(err.Error())
}
}

type rollbackReq struct {
AppName *string `json:"appName" binding:"required"`
Deployment *string `json:"deployment" binding:"required"`
Version *string `json:"version"`
}

func (App) Rollback(ctx *gin.Context) {
rollbackReq := rollbackReq{}
if err := ctx.ShouldBindBodyWith(&rollbackReq, binding.JSON); err == nil {
uid := ctx.MustGet(constants.GIN_USER_ID).(int)

app := model.App{}.GetAppByUidAndAppName(uid, *rollbackReq.AppName)
if app == nil {
log.Panic("App not found")
}
deployment := model.Deployment{}.GetByAppidAndName(*app.Id, *rollbackReq.Deployment)
if deployment == nil {
log.Panic("Deployment " + *rollbackReq.Deployment + " not found")
}

var deploymentVersion *model.DeploymentVersion
if deployment.VersionId != nil {
deploymentVersion = model.DeploymentVersion{}.GetByKeyDeploymentIdAndVersion(*deployment.Id, *rollbackReq.Version)
}
if deploymentVersion == nil {
log.Panic("Version not found")
}
if deploymentVersion.CurrentPackage == nil {
log.Panic("There is no upload package for the current version")
}
newPackage := model.Package{}.GetRollbackPack(*deployment.Id, *deploymentVersion.CurrentPackage, *deploymentVersion.Id)

userDb, _ := db.GetUserDB()
err := userDb.Transaction(func(tx *gorm.DB) error {
if newPackage == nil {
model.DeploymentVersion{}.UpdateCurrentPackage(*deploymentVersion.Id, nil)
} else {
model.DeploymentVersion{}.UpdateCurrentPackage(*deploymentVersion.Id, newPackage.Id)
}
return nil
})
if err != nil {
panic("RollbackError:" + err.Error())
}

if newPackage != nil {
ctx.JSON(http.StatusOK, gin.H{
"Success": true,
"Version": *deploymentVersion.AppVersion,
"PackId": *newPackage.Id,
"Size": *newPackage.Size,
"Hash": *newPackage.Hash,
"CreateTime": *newPackage.CreateTime,
})
} else {
ctx.JSON(http.StatusOK, gin.H{
"Success": true,
"Version": *deploymentVersion.AppVersion,
})
}
redis.DelRedisObj(constants.REDIS_UPDATE_INFO + *deployment.Key + "*")
} else {
log.Panic(err.Error())
}
}
30 changes: 17 additions & 13 deletions request/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

type Client struct{}
type updateInfo struct {
DownloadUrl string `json:"download_url"`
DownloadUrl string `json:"download_url"`
Description string `json:"description"`
IsAvailable bool `json:"is_available"`
IsDisabled bool `json:"is_disabled"`
Expand Down Expand Up @@ -51,18 +51,22 @@ func (Client) CheckUpdate(ctx *gin.Context) {
}
deploymentVersion := model.DeploymentVersion{}.GetByKeyDeploymentIdAndVersion(*deployment.Id, appVersion)
if deploymentVersion != 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
updateInfoRedis.Description = *packag.Description
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)
Expand Down

0 comments on commit 9d8410b

Please sign in to comment.