fix: 解决网站一键部署的BUG

This commit is contained in:
zhengkunwang223 2022-12-22 15:26:25 +08:00 committed by zhengkunwang223
parent 8ddaef68cf
commit e33bf51941
5 changed files with 21 additions and 17 deletions

View file

@ -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)
}

View file

@ -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
}

View file

@ -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

View file

@ -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
}

View file

@ -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: