diff --git a/backend/app/service/website.go b/backend/app/service/website.go index 7cd371196..5628df033 100644 --- a/backend/app/service/website.go +++ b/backend/app/service/website.go @@ -92,6 +92,8 @@ func (w WebsiteService) CreateWebsite(create request.WebsiteCreate) error { Proxy: create.Proxy, } + tx, ctx := getTxAndContext() + switch create.Type { case constant.Deployment: if create.AppType == constant.NewApp { @@ -99,19 +101,14 @@ 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(context.Background(), req) + install, err := ServiceGroupApp.Install(ctx, req) if err != nil { return err } website.AppInstallID = install.ID } - case constant.Static: - if err := createStaticHtml(website); err != nil { - return err - } } - tx, ctx := getTxAndContext() if err := websiteRepo.Create(ctx, website); err != nil { return err } diff --git a/backend/app/service/website_utils.go b/backend/app/service/website_utils.go index 5cc29b0b9..ad29d8954 100644 --- a/backend/app/service/website_utils.go +++ b/backend/app/service/website_utils.go @@ -94,6 +94,11 @@ func createWebsiteFolder(nginxInstall model.AppInstall, website *model.Website) if err := fileOp.CreateDir(path.Join(siteFolder, "ssl"), 0755); err != nil { return err } + if website.Type == constant.Static { + if err := createStaticHtml(website); err != nil { + return err + } + } } return fileOp.CopyDir(path.Join(nginxFolder, "www", "common", "waf", "rules"), path.Join(siteFolder, "waf")) } @@ -151,9 +156,15 @@ func configDefaultNginx(website *model.Website, domains []model.WebsiteDomain) e } if err := opNginx(nginxInstall.ContainerName, constant.NginxCheck); err != nil { + _ = deleteWebsiteFolder(nginxInstall, website) return err } - return opNginx(nginxInstall.ContainerName, constant.NginxReload) + if err := opNginx(nginxInstall.ContainerName, constant.NginxReload); err != nil { + _ = deleteWebsiteFolder(nginxInstall, website) + return err + } + + return nil } func delNginxConfig(website model.Website, force bool) error { @@ -564,3 +575,17 @@ func copyConf(srcPath, dstPath string) error { _, _ = io.Copy(out, src) return nil } + +func deleteWebsiteFolder(nginxInstall model.AppInstall, website *model.Website) error { + nginxFolder := path.Join(constant.AppInstallDir, constant.AppNginx, nginxInstall.Name) + siteFolder := path.Join(nginxFolder, "www", "sites", website.Alias) + fileOp := files.NewFileOp() + if fileOp.Stat(siteFolder) { + _ = fileOp.DeleteDir(siteFolder) + } + nginxFilePath := path.Join(nginxFolder, "conf", "conf.d", website.PrimaryDomain+".conf") + if fileOp.Stat(nginxFilePath) { + _ = fileOp.DeleteFile(nginxFilePath) + } + return nil +}