From e33bf51941cb72df4992ea799a7a5a276aa8a019 Mon Sep 17 00:00:00 2001 From: zhengkunwang223 Date: Thu, 22 Dec 2022 15:26:25 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E7=BD=91=E7=AB=99?= =?UTF-8?q?=E4=B8=80=E9=94=AE=E9=83=A8=E7=BD=B2=E7=9A=84BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/v1/app.go | 6 ++++-- backend/app/api/v1/helper/helper.go | 9 +++++++++ backend/app/service/app.go | 4 ---- backend/app/service/website.go | 13 +++++++------ backend/app/service/website_utils.go | 6 +----- 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/backend/app/api/v1/app.go b/backend/app/api/v1/app.go index 9db46eaa6..553e84e43 100644 --- a/backend/app/api/v1/app.go +++ b/backend/app/api/v1/app.go @@ -1,7 +1,6 @@ package v1 import ( - "context" "github.com/1Panel-dev/1Panel/backend/app/api/v1/helper" "github.com/1Panel-dev/1Panel/backend/app/dto/request" "github.com/1Panel-dev/1Panel/backend/constant" @@ -64,10 +63,13 @@ func (b *BaseApi) InstallApp(c *gin.Context) { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return } - install, err := appService.Install(context.Background(), req) + tx, ctx := helper.GetTxAndContext() + install, err := appService.Install(ctx, req) if err != nil { + tx.Rollback() helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return } + tx.Commit() helper.SuccessWithData(c, install) } diff --git a/backend/app/api/v1/helper/helper.go b/backend/app/api/v1/helper/helper.go index 733103c49..cd8e4acf9 100644 --- a/backend/app/api/v1/helper/helper.go +++ b/backend/app/api/v1/helper/helper.go @@ -1,7 +1,10 @@ package helper import ( + "context" "fmt" + "github.com/1Panel-dev/1Panel/backend/global" + "gorm.io/gorm" "net/http" "strconv" @@ -103,3 +106,9 @@ func GetIntParamByKey(c *gin.Context, key string) (uint, error) { intNum, _ := strconv.Atoi(idParam) return uint(intNum), nil } + +func GetTxAndContext() (tx *gorm.DB, ctx context.Context) { + tx = global.DB.Begin() + ctx = context.WithValue(context.Background(), constant.DB, tx) + return +} diff --git a/backend/app/service/app.go b/backend/app/service/app.go index 762f3c045..8513a8679 100644 --- a/backend/app/service/app.go +++ b/backend/app/service/app.go @@ -215,16 +215,12 @@ func (a AppService) Install(ctx context.Context, req request.AppInstallCreate) ( return nil, err } - tx, ctx := getTxByContext(ctx) if err := appInstallRepo.Create(ctx, &appInstall); err != nil { - tx.Rollback() return nil, err } if err := createLink(ctx, app, &appInstall, req.Params); err != nil { - tx.Rollback() return nil, err } - tx.Commit() go upApp(appInstall.GetComposePath(), appInstall) go updateToolApp(appInstall) return &appInstall, nil diff --git a/backend/app/service/website.go b/backend/app/service/website.go index 5628df033..87211354a 100644 --- a/backend/app/service/website.go +++ b/backend/app/service/website.go @@ -93,7 +93,7 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) error { } tx, ctx := getTxAndContext() - + var appInstall *model.AppInstall switch create.Type { case constant.Deployment: if create.AppType == constant.NewApp { @@ -101,11 +101,12 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) error { req.Name = create.AppInstall.Name req.AppDetailId = create.AppInstall.AppDetailId req.Params = create.AppInstall.Params - install, err := ServiceGroupApp.Install(ctx, req) - if err != nil { - return err + var installErr error + appInstall, installErr = ServiceGroupApp.Install(ctx, req) + if installErr != nil { + return installErr } - website.AppInstallID = install.ID + website.AppInstallID = appInstall.ID } } @@ -137,7 +138,7 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) error { } } - if err := configDefaultNginx(website, domains); err != nil { + if err := configDefaultNginx(website, domains, appInstall); err != nil { tx.Rollback() return err } diff --git a/backend/app/service/website_utils.go b/backend/app/service/website_utils.go index e6c0a8504..eaaf1674e 100644 --- a/backend/app/service/website_utils.go +++ b/backend/app/service/website_utils.go @@ -102,7 +102,7 @@ func createWebsiteFolder(nginxInstall model.AppInstall, website *model.Website) return fileOp.CopyDir(path.Join(nginxFolder, "www", "common", "waf", "rules"), path.Join(siteFolder, "waf")) } -func configDefaultNginx(website *model.Website, domains []model.WebsiteDomain) error { +func configDefaultNginx(website *model.Website, domains []model.WebsiteDomain, appInstall *model.AppInstall) error { nginxInstall, err := getAppInstallByKey(constant.AppNginx) if err != nil { return err @@ -136,10 +136,6 @@ func configDefaultNginx(website *model.Website, domains []model.WebsiteDomain) e switch website.Type { case constant.Deployment: - appInstall, err := appInstallRepo.GetFirst(commonRepo.WithByID(website.AppInstallID)) - if err != nil { - return err - } proxy := fmt.Sprintf("http://127.0.0.1:%d", appInstall.HttpPort) server.UpdateRootProxy([]string{proxy}) case constant.Static: