2022-09-22 08:16:04 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2022-12-21 07:54:34 +00:00
|
|
|
"context"
|
2022-09-22 08:16:04 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
2022-12-02 02:31:07 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
2022-11-08 06:34:41 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
2022-10-17 08:32:31 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
2022-12-14 07:08:21 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto/response"
|
2022-10-17 08:32:31 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/repo"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/common"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/docker"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
2022-09-29 10:16:56 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2022-09-22 08:16:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type AppService struct {
|
|
|
|
}
|
|
|
|
|
2022-12-14 07:08:21 +00:00
|
|
|
func (a AppService) PageApp(req request.AppSearch) (interface{}, error) {
|
2022-09-22 08:16:04 +00:00
|
|
|
var opts []repo.DBOption
|
|
|
|
opts = append(opts, commonRepo.WithOrderBy("name"))
|
|
|
|
if req.Name != "" {
|
|
|
|
opts = append(opts, commonRepo.WithLikeName(req.Name))
|
|
|
|
}
|
2022-11-02 07:19:14 +00:00
|
|
|
if req.Type != "" {
|
|
|
|
opts = append(opts, appRepo.WithType(req.Type))
|
|
|
|
}
|
2022-09-23 08:33:55 +00:00
|
|
|
if len(req.Tags) != 0 {
|
|
|
|
tags, err := tagRepo.GetByKeys(req.Tags)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var tagIds []uint
|
|
|
|
for _, t := range tags {
|
|
|
|
tagIds = append(tagIds, t.ID)
|
|
|
|
}
|
|
|
|
appTags, err := appTagRepo.GetByTagIds(tagIds)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var appIds []uint
|
|
|
|
for _, t := range appTags {
|
|
|
|
appIds = append(appIds, t.AppId)
|
|
|
|
}
|
|
|
|
|
|
|
|
opts = append(opts, commonRepo.WithIdsIn(appIds))
|
2022-09-22 08:16:04 +00:00
|
|
|
}
|
2022-12-14 07:08:21 +00:00
|
|
|
var res response.AppRes
|
2022-09-22 08:16:04 +00:00
|
|
|
total, apps, err := appRepo.Page(req.Page, req.PageSize, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-14 07:08:21 +00:00
|
|
|
var appDTOs []*response.AppDTO
|
2022-09-22 08:16:04 +00:00
|
|
|
for _, a := range apps {
|
2022-12-14 07:08:21 +00:00
|
|
|
appDTO := &response.AppDTO{
|
2022-09-22 08:16:04 +00:00
|
|
|
App: a,
|
|
|
|
}
|
|
|
|
appDTOs = append(appDTOs, appDTO)
|
|
|
|
appTags, err := appTagRepo.GetByAppId(a.ID)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var tagIds []uint
|
|
|
|
for _, at := range appTags {
|
|
|
|
tagIds = append(tagIds, at.TagId)
|
|
|
|
}
|
|
|
|
tags, err := tagRepo.GetByIds(tagIds)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
appDTO.Tags = tags
|
|
|
|
}
|
|
|
|
res.Items = appDTOs
|
|
|
|
res.Total = total
|
|
|
|
tags, err := tagRepo.All()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res.Tags = tags
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2022-12-14 07:08:21 +00:00
|
|
|
func (a AppService) GetApp(id uint) (response.AppDTO, error) {
|
|
|
|
var appDTO response.AppDTO
|
2022-09-23 08:33:55 +00:00
|
|
|
app, err := appRepo.GetFirst(commonRepo.WithByID(id))
|
|
|
|
if err != nil {
|
|
|
|
return appDTO, err
|
|
|
|
}
|
|
|
|
appDTO.App = app
|
2022-10-03 09:35:39 +00:00
|
|
|
details, err := appDetailRepo.GetBy(appDetailRepo.WithAppId(app.ID))
|
2022-09-23 08:33:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return appDTO, err
|
|
|
|
}
|
|
|
|
var versionsRaw []string
|
|
|
|
for _, detail := range details {
|
|
|
|
versionsRaw = append(versionsRaw, detail.Version)
|
|
|
|
}
|
|
|
|
|
2022-10-03 09:35:39 +00:00
|
|
|
appDTO.Versions = common.GetSortedVersions(versionsRaw)
|
2022-09-23 08:33:55 +00:00
|
|
|
|
|
|
|
return appDTO, nil
|
|
|
|
}
|
|
|
|
|
2022-12-14 07:08:21 +00:00
|
|
|
func (a AppService) GetAppDetail(appId uint, version string) (response.AppDetailDTO, error) {
|
2022-09-23 08:33:55 +00:00
|
|
|
var (
|
2022-12-14 07:08:21 +00:00
|
|
|
appDetailDTO response.AppDetailDTO
|
2022-09-30 09:56:06 +00:00
|
|
|
opts []repo.DBOption
|
2022-09-23 08:33:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
opts = append(opts, appDetailRepo.WithAppId(appId), appDetailRepo.WithVersion(version))
|
2022-10-03 09:35:39 +00:00
|
|
|
detail, err := appDetailRepo.GetFirst(opts...)
|
2022-09-23 08:33:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return appDetailDTO, err
|
|
|
|
}
|
2022-09-26 08:32:40 +00:00
|
|
|
paramMap := make(map[string]interface{})
|
2022-12-26 08:33:54 +00:00
|
|
|
if err := json.Unmarshal([]byte(detail.Params), ¶mMap); err != nil {
|
|
|
|
return appDetailDTO, err
|
|
|
|
}
|
2022-09-23 08:33:55 +00:00
|
|
|
appDetailDTO.AppDetail = detail
|
2022-09-26 08:32:40 +00:00
|
|
|
appDetailDTO.Params = paramMap
|
2022-12-01 08:45:00 +00:00
|
|
|
appDetailDTO.Enable = true
|
|
|
|
|
|
|
|
app, err := appRepo.GetFirst(commonRepo.WithByID(detail.AppId))
|
|
|
|
if err != nil {
|
|
|
|
return appDetailDTO, err
|
|
|
|
}
|
|
|
|
if err := checkLimit(app); err != nil {
|
|
|
|
appDetailDTO.Enable = false
|
|
|
|
}
|
|
|
|
|
2022-09-23 08:33:55 +00:00
|
|
|
return appDetailDTO, nil
|
|
|
|
}
|
|
|
|
|
2022-12-21 07:54:34 +00:00
|
|
|
func (a AppService) Install(ctx context.Context, req request.AppInstallCreate) (*model.AppInstall, error) {
|
|
|
|
if list, _ := appInstallRepo.GetBy(commonRepo.WithByName(req.Name)); len(list) > 0 {
|
2022-12-04 04:47:19 +00:00
|
|
|
return nil, buserr.New(constant.ErrNameIsExist)
|
2022-12-02 02:31:07 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 07:54:34 +00:00
|
|
|
httpPort, err := checkPort("PANEL_APP_PORT_HTTP", req.Params)
|
2022-10-11 08:27:58 +00:00
|
|
|
if err != nil {
|
2022-12-01 07:48:40 +00:00
|
|
|
return nil, err
|
2022-10-11 08:27:58 +00:00
|
|
|
}
|
2022-12-21 07:54:34 +00:00
|
|
|
httpsPort, err := checkPort("PANEL_APP_PORT_HTTPS", req.Params)
|
2022-10-11 08:27:58 +00:00
|
|
|
if err != nil {
|
2022-12-01 07:48:40 +00:00
|
|
|
return nil, err
|
2022-09-27 08:57:23 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 07:54:34 +00:00
|
|
|
appDetail, err := appDetailRepo.GetFirst(commonRepo.WithByID(req.AppDetailId))
|
2022-09-26 08:32:40 +00:00
|
|
|
if err != nil {
|
2022-11-02 07:19:14 +00:00
|
|
|
return nil, err
|
2022-09-26 08:32:40 +00:00
|
|
|
}
|
|
|
|
app, err := appRepo.GetFirst(commonRepo.WithByID(appDetail.AppId))
|
2022-10-07 07:49:39 +00:00
|
|
|
if err != nil {
|
2022-11-02 07:19:14 +00:00
|
|
|
return nil, err
|
2022-10-07 07:49:39 +00:00
|
|
|
}
|
2022-10-09 15:35:24 +00:00
|
|
|
|
2022-10-10 14:56:42 +00:00
|
|
|
if err := checkRequiredAndLimit(app); err != nil {
|
2022-11-02 07:19:14 +00:00
|
|
|
return nil, err
|
2022-10-10 14:56:42 +00:00
|
|
|
}
|
2022-12-21 07:54:34 +00:00
|
|
|
if err := copyAppData(app.Key, appDetail.Version, req.Name, req.Params); err != nil {
|
2022-11-02 07:19:14 +00:00
|
|
|
return nil, err
|
2022-10-07 07:49:39 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 07:54:34 +00:00
|
|
|
paramByte, err := json.Marshal(req.Params)
|
2022-09-26 08:32:40 +00:00
|
|
|
if err != nil {
|
2022-11-02 07:19:14 +00:00
|
|
|
return nil, err
|
2022-09-26 08:32:40 +00:00
|
|
|
}
|
|
|
|
appInstall := model.AppInstall{
|
2022-12-21 07:54:34 +00:00
|
|
|
Name: req.Name,
|
2022-09-29 10:16:56 +00:00
|
|
|
AppId: appDetail.AppId,
|
|
|
|
AppDetailId: appDetail.ID,
|
|
|
|
Version: appDetail.Version,
|
|
|
|
Status: constant.Installing,
|
2022-10-11 08:27:58 +00:00
|
|
|
Env: string(paramByte),
|
|
|
|
HttpPort: httpPort,
|
|
|
|
HttpsPort: httpsPort,
|
|
|
|
App: app,
|
2022-10-09 15:35:24 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 10:16:56 +00:00
|
|
|
composeMap := make(map[string]interface{})
|
2022-10-07 07:49:39 +00:00
|
|
|
if err := yaml.Unmarshal([]byte(appDetail.DockerCompose), &composeMap); err != nil {
|
2022-11-02 07:19:14 +00:00
|
|
|
return nil, err
|
2022-09-29 10:16:56 +00:00
|
|
|
}
|
|
|
|
servicesMap := composeMap["services"].(map[string]interface{})
|
|
|
|
changeKeys := make(map[string]string, len(servicesMap))
|
|
|
|
for k, v := range servicesMap {
|
|
|
|
serviceName := k + "-" + common.RandStr(4)
|
|
|
|
changeKeys[k] = serviceName
|
|
|
|
value := v.(map[string]interface{})
|
|
|
|
containerName := constant.ContainerPrefix + k + "-" + common.RandStr(4)
|
|
|
|
value["container_name"] = containerName
|
2022-10-11 08:27:58 +00:00
|
|
|
appInstall.ServiceName = serviceName
|
|
|
|
appInstall.ContainerName = containerName
|
2022-09-29 10:16:56 +00:00
|
|
|
}
|
|
|
|
for k, v := range changeKeys {
|
|
|
|
servicesMap[v] = servicesMap[k]
|
|
|
|
delete(servicesMap, k)
|
|
|
|
}
|
2022-10-07 07:49:39 +00:00
|
|
|
composeByte, err := yaml.Marshal(composeMap)
|
2022-09-29 10:16:56 +00:00
|
|
|
if err != nil {
|
2022-11-02 07:19:14 +00:00
|
|
|
return nil, err
|
2022-09-29 10:16:56 +00:00
|
|
|
}
|
2022-10-11 08:27:58 +00:00
|
|
|
appInstall.DockerCompose = string(composeByte)
|
|
|
|
|
2022-10-10 07:10:53 +00:00
|
|
|
fileOp := files.NewFileOp()
|
2022-10-11 08:27:58 +00:00
|
|
|
if err := fileOp.WriteFile(appInstall.GetComposePath(), strings.NewReader(string(composeByte)), 0775); err != nil {
|
2022-11-02 07:19:14 +00:00
|
|
|
return nil, err
|
2022-09-29 10:16:56 +00:00
|
|
|
}
|
|
|
|
|
2022-10-09 15:35:24 +00:00
|
|
|
if err := appInstallRepo.Create(ctx, &appInstall); err != nil {
|
2022-11-02 07:19:14 +00:00
|
|
|
return nil, err
|
2022-09-26 08:32:40 +00:00
|
|
|
}
|
2022-12-21 07:54:34 +00:00
|
|
|
if err := createLink(ctx, app, &appInstall, req.Params); err != nil {
|
2022-11-02 07:19:14 +00:00
|
|
|
return nil, err
|
2022-09-29 10:16:56 +00:00
|
|
|
}
|
2022-10-11 08:27:58 +00:00
|
|
|
go upApp(appInstall.GetComposePath(), appInstall)
|
2022-12-09 08:03:00 +00:00
|
|
|
go updateToolApp(appInstall)
|
2022-11-02 07:19:14 +00:00
|
|
|
return &appInstall, nil
|
2022-09-26 08:32:40 +00:00
|
|
|
}
|
|
|
|
|
2022-09-29 10:16:56 +00:00
|
|
|
func (a AppService) SyncInstalled(installId uint) error {
|
|
|
|
appInstall, err := appInstallRepo.GetFirst(commonRepo.WithByID(installId))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-11 08:27:58 +00:00
|
|
|
|
|
|
|
containerNames, err := getContainerNames(appInstall)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-09-29 10:16:56 +00:00
|
|
|
}
|
2022-10-11 08:27:58 +00:00
|
|
|
|
2022-09-29 10:16:56 +00:00
|
|
|
cli, err := docker.NewClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
containers, err := cli.ListContainersByName(containerNames)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-30 09:56:06 +00:00
|
|
|
var (
|
|
|
|
errorContainers []string
|
|
|
|
notFoundContainers []string
|
|
|
|
runningContainers []string
|
|
|
|
)
|
2022-09-29 10:16:56 +00:00
|
|
|
|
|
|
|
for _, n := range containers {
|
|
|
|
if n.State != "running" {
|
2022-09-30 09:56:06 +00:00
|
|
|
errorContainers = append(errorContainers, n.Names[0])
|
|
|
|
} else {
|
|
|
|
runningContainers = append(runningContainers, n.Names[0])
|
2022-09-29 10:16:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, old := range containerNames {
|
|
|
|
exist := false
|
|
|
|
for _, new := range containers {
|
|
|
|
if common.ExistWithStrArray(old, new.Names) {
|
|
|
|
exist = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !exist {
|
|
|
|
notFoundContainers = append(notFoundContainers, old)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 09:56:06 +00:00
|
|
|
containerCount := len(containers)
|
|
|
|
errCount := len(errorContainers)
|
|
|
|
notFoundCount := len(notFoundContainers)
|
|
|
|
normalCount := len(containerNames)
|
|
|
|
runningCount := len(runningContainers)
|
|
|
|
|
|
|
|
if containerCount == 0 {
|
2022-09-29 10:16:56 +00:00
|
|
|
appInstall.Status = constant.Error
|
|
|
|
appInstall.Message = "container is not found"
|
2022-10-13 08:46:38 +00:00
|
|
|
return appInstallRepo.Save(&appInstall)
|
2022-09-29 10:16:56 +00:00
|
|
|
}
|
2022-09-30 09:56:06 +00:00
|
|
|
if errCount == 0 && notFoundCount == 0 {
|
2022-09-29 10:16:56 +00:00
|
|
|
appInstall.Status = constant.Running
|
2022-10-13 08:46:38 +00:00
|
|
|
return appInstallRepo.Save(&appInstall)
|
2022-09-29 10:16:56 +00:00
|
|
|
}
|
2022-09-30 09:56:06 +00:00
|
|
|
if errCount == normalCount {
|
2022-09-29 10:16:56 +00:00
|
|
|
appInstall.Status = constant.Error
|
|
|
|
}
|
2022-09-30 09:56:06 +00:00
|
|
|
if notFoundCount == normalCount {
|
2022-09-29 10:16:56 +00:00
|
|
|
appInstall.Status = constant.Stopped
|
|
|
|
}
|
2022-09-30 09:56:06 +00:00
|
|
|
if runningCount < normalCount {
|
|
|
|
appInstall.Status = constant.UnHealthy
|
|
|
|
}
|
2022-09-29 10:16:56 +00:00
|
|
|
|
|
|
|
var errMsg strings.Builder
|
2022-09-30 09:56:06 +00:00
|
|
|
if errCount > 0 {
|
|
|
|
errMsg.Write([]byte(string(rune(errCount)) + " error containers:"))
|
2022-09-29 10:16:56 +00:00
|
|
|
for _, e := range errorContainers {
|
|
|
|
errMsg.Write([]byte(e))
|
|
|
|
}
|
|
|
|
errMsg.Write([]byte("\n"))
|
|
|
|
}
|
2022-09-30 09:56:06 +00:00
|
|
|
if notFoundCount > 0 {
|
|
|
|
errMsg.Write([]byte(string(rune(notFoundCount)) + " not found containers:"))
|
2022-09-29 10:16:56 +00:00
|
|
|
for _, e := range notFoundContainers {
|
|
|
|
errMsg.Write([]byte(e))
|
|
|
|
}
|
|
|
|
errMsg.Write([]byte("\n"))
|
|
|
|
}
|
|
|
|
appInstall.Message = errMsg.String()
|
2022-10-13 08:46:38 +00:00
|
|
|
return appInstallRepo.Save(&appInstall)
|
2022-09-29 10:16:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a AppService) SyncAppList() error {
|
2022-11-18 10:02:14 +00:00
|
|
|
if err := getAppFromOss(); err != nil {
|
|
|
|
global.LOG.Errorf("get app from oss error: %s", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
2022-09-22 08:16:04 +00:00
|
|
|
|
2022-10-12 10:57:22 +00:00
|
|
|
appDir := constant.AppResourceDir
|
2022-09-22 08:16:04 +00:00
|
|
|
listFile := path.Join(appDir, "list.json")
|
|
|
|
|
|
|
|
content, err := os.ReadFile(listFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
list := &dto.AppList{}
|
|
|
|
if err := json.Unmarshal(content, list); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2022-09-30 09:56:06 +00:00
|
|
|
tags []*model.Tag
|
|
|
|
appTags []*model.AppTag
|
2022-09-22 08:16:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
for _, t := range list.Tags {
|
|
|
|
tags = append(tags, &model.Tag{
|
|
|
|
Key: t.Key,
|
|
|
|
Name: t.Name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-30 09:56:06 +00:00
|
|
|
oldApps, err := appRepo.GetBy()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
appsMap := getApps(oldApps, list.Items)
|
|
|
|
|
2022-09-22 08:16:04 +00:00
|
|
|
for _, l := range list.Items {
|
2022-09-30 09:56:06 +00:00
|
|
|
|
|
|
|
app := appsMap[l.Key]
|
2022-11-18 06:27:40 +00:00
|
|
|
icon, err := os.ReadFile(path.Join(appDir, l.Key, "metadata", "logo.png"))
|
2022-09-22 08:16:04 +00:00
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("get [%s] icon error: %s", l.Name, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
iconStr := base64.StdEncoding.EncodeToString(icon)
|
2022-09-30 09:56:06 +00:00
|
|
|
app.Icon = iconStr
|
2022-09-22 08:16:04 +00:00
|
|
|
app.TagsKey = l.Tags
|
|
|
|
|
|
|
|
versions := l.Versions
|
2022-09-30 09:56:06 +00:00
|
|
|
detailsMap := getAppDetails(app.Details, versions)
|
|
|
|
|
2022-09-22 08:16:04 +00:00
|
|
|
for _, v := range versions {
|
2022-09-30 09:56:06 +00:00
|
|
|
detail := detailsMap[v]
|
2022-11-18 06:27:40 +00:00
|
|
|
detailPath := path.Join(appDir, l.Key, "versions", v)
|
2022-09-22 08:16:04 +00:00
|
|
|
if _, err := os.Stat(detailPath); err != nil {
|
|
|
|
global.LOG.Errorf("get [%s] folder error: %s", detailPath, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
readmeStr, err := os.ReadFile(path.Join(detailPath, "README.md"))
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("get [%s] README error: %s", detailPath, err.Error())
|
|
|
|
}
|
|
|
|
detail.Readme = string(readmeStr)
|
|
|
|
dockerComposeStr, err := os.ReadFile(path.Join(detailPath, "docker-compose.yml"))
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("get [%s] docker-compose.yml error: %s", detailPath, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
detail.DockerCompose = string(dockerComposeStr)
|
2022-10-13 08:46:38 +00:00
|
|
|
paramStr, err := os.ReadFile(path.Join(detailPath, "config.json"))
|
2022-09-22 08:16:04 +00:00
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("get [%s] form.json error: %s", detailPath, err.Error())
|
|
|
|
}
|
2022-09-26 08:32:40 +00:00
|
|
|
detail.Params = string(paramStr)
|
2022-09-30 09:56:06 +00:00
|
|
|
detailsMap[v] = detail
|
|
|
|
}
|
|
|
|
var newDetails []model.AppDetail
|
|
|
|
for _, v := range detailsMap {
|
|
|
|
newDetails = append(newDetails, v)
|
2022-09-22 08:16:04 +00:00
|
|
|
}
|
2022-09-30 09:56:06 +00:00
|
|
|
app.Details = newDetails
|
|
|
|
appsMap[l.Key] = app
|
2022-09-22 08:16:04 +00:00
|
|
|
}
|
2022-09-30 09:56:06 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
addAppArray []model.App
|
|
|
|
updateArray []model.App
|
|
|
|
)
|
|
|
|
tagMap := make(map[string]uint, len(tags))
|
|
|
|
for _, v := range appsMap {
|
|
|
|
if v.ID == 0 {
|
|
|
|
addAppArray = append(addAppArray, v)
|
|
|
|
} else {
|
|
|
|
updateArray = append(updateArray, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 06:27:40 +00:00
|
|
|
tx, ctx := getTxAndContext()
|
2022-09-30 09:56:06 +00:00
|
|
|
|
|
|
|
if len(addAppArray) > 0 {
|
|
|
|
if err := appRepo.BatchCreate(ctx, addAppArray); err != nil {
|
2022-09-22 08:16:04 +00:00
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := tagRepo.DeleteAll(ctx); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(tags) > 0 {
|
|
|
|
if err := tagRepo.BatchCreate(ctx, tags); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
2022-09-30 09:56:06 +00:00
|
|
|
for _, t := range tags {
|
|
|
|
tagMap[t.Key] = t.ID
|
|
|
|
}
|
2022-09-22 08:16:04 +00:00
|
|
|
}
|
2022-09-30 09:56:06 +00:00
|
|
|
for _, update := range updateArray {
|
|
|
|
if err := appRepo.Save(ctx, &update); err != nil {
|
2022-09-22 08:16:04 +00:00
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-09-30 09:56:06 +00:00
|
|
|
|
|
|
|
apps := append(addAppArray, updateArray...)
|
2022-09-22 08:16:04 +00:00
|
|
|
|
|
|
|
var (
|
2022-09-30 09:56:06 +00:00
|
|
|
addDetails []model.AppDetail
|
|
|
|
updateDetails []model.AppDetail
|
2022-09-22 08:16:04 +00:00
|
|
|
)
|
|
|
|
for _, a := range apps {
|
|
|
|
for _, t := range a.TagsKey {
|
|
|
|
tagId, ok := tagMap[t]
|
|
|
|
if ok {
|
|
|
|
appTags = append(appTags, &model.AppTag{
|
|
|
|
AppId: a.ID,
|
|
|
|
TagId: tagId,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range a.Details {
|
|
|
|
d.AppId = a.ID
|
2022-09-30 09:56:06 +00:00
|
|
|
if d.ID == 0 {
|
|
|
|
addDetails = append(addDetails, d)
|
|
|
|
} else {
|
|
|
|
updateDetails = append(updateDetails, d)
|
|
|
|
}
|
2022-09-22 08:16:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-30 09:56:06 +00:00
|
|
|
if len(addDetails) > 0 {
|
|
|
|
if err := appDetailRepo.BatchCreate(ctx, addDetails); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
2022-09-22 08:16:04 +00:00
|
|
|
}
|
2022-09-30 09:56:06 +00:00
|
|
|
for _, u := range updateDetails {
|
|
|
|
if err := appDetailRepo.Update(ctx, u); err != nil {
|
2022-09-22 08:16:04 +00:00
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-09-30 09:56:06 +00:00
|
|
|
if err := appTagRepo.DeleteAll(ctx); err != nil {
|
2022-09-22 08:16:04 +00:00
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(appTags) > 0 {
|
|
|
|
if err := appTagRepo.BatchCreate(ctx, appTags); err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tx.Commit()
|
|
|
|
return nil
|
|
|
|
}
|