From a353adc4b9a24a521077a6e2fa32c8e3c3e4b513 Mon Sep 17 00:00:00 2001 From: zhengkunwang223 Date: Tue, 13 Dec 2022 20:03:54 +0800 Subject: [PATCH] =?UTF-8?q?style:=20=E4=BF=AE=E6=94=B9nginx=E7=9A=84consta?= =?UTF-8?q?nt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/app_install.go | 10 +++++----- backend/app/service/nginx.go | 6 +++--- backend/app/service/nginx_utils.go | 4 ++-- backend/app/service/website.go | 6 +++--- backend/app/service/website_ssl.go | 8 ++++---- backend/app/service/website_utils.go | 24 ++++++++++++------------ backend/buserr/errors.go | 1 - backend/constant/app.go | 4 ++++ 8 files changed, 33 insertions(+), 30 deletions(-) diff --git a/backend/app/service/app_install.go b/backend/app/service/app_install.go index 3f78127e4..67eb52981 100644 --- a/backend/app/service/app_install.go +++ b/backend/app/service/app_install.go @@ -285,7 +285,7 @@ func (a AppInstallService) DeleteCheck(installId uint) ([]dto.AppResource, error }) } } - if app.Key == "nginx" { + if app.Key == constant.AppNginx { websites, _ := websiteRepo.GetBy() for _, website := range websites { res = append(res, dto.AppResource{ @@ -305,7 +305,7 @@ func (a AppInstallService) DeleteCheck(installId uint) ([]dto.AppResource, error } } - if app.Key == "mysql" { + if app.Key == constant.AppMysql { databases, _ := mysqlRepo.List() for _, database := range databases { res = append(res, dto.AppResource{ @@ -324,13 +324,13 @@ func (a AppInstallService) GetDefaultConfigByKey(key string) (string, error) { return "", err } filePath := path.Join(constant.AppResourceDir, appInstall.App.Key, "versions", appInstall.Version, "conf") - if key == "mysql" { + if key == constant.AppMysql { filePath = path.Join(filePath, "my.cnf") } - if key == "redis" { + if key == constant.AppRedis { filePath = path.Join(filePath, "redis.conf") } - if key == "nginx" { + if key == constant.AppNginx { filePath = path.Join(filePath, "nginx.conf") } contentByte, err := os.ReadFile(filePath) diff --git a/backend/app/service/nginx.go b/backend/app/service/nginx.go index f03a0cc3b..2445c2347 100644 --- a/backend/app/service/nginx.go +++ b/backend/app/service/nginx.go @@ -19,11 +19,11 @@ type NginxService struct { } func (n NginxService) GetNginxConfig() (dto.FileInfo, error) { - nginxInstall, err := getAppInstallByKey("nginx") + nginxInstall, err := getAppInstallByKey(constant.AppNginx) if err != nil { return dto.FileInfo{}, err } - configPath := path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name, "conf", "nginx.conf") + configPath := path.Join(constant.AppInstallDir, constant.AppNginx, nginxInstall.Name, "conf", "nginx.conf") info, err := files.NewFileInfo(files.FileOption{ Path: configPath, Expand: true, @@ -95,7 +95,7 @@ func (n NginxService) UpdateConfigFile(req request.NginxConfigFileUpdate) error if err := fileOp.WriteFile(req.FilePath, strings.NewReader(req.Content), 0644); err != nil { return err } - nginxInstall, err := getAppInstallByKey("nginx") + nginxInstall, err := getAppInstallByKey(constant.AppNginx) if err != nil { return err } diff --git a/backend/app/service/nginx_utils.go b/backend/app/service/nginx_utils.go index c54cc9b05..10d65e76e 100644 --- a/backend/app/service/nginx_utils.go +++ b/backend/app/service/nginx_utils.go @@ -25,7 +25,7 @@ func getNginxFull(website *model.Website) (dto.NginxFull, error) { return nginxFull, err } nginxFull.Install = nginxInstall - nginxFull.Dir = path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name) + nginxFull.Dir = path.Join(constant.AppInstallDir, constant.AppNginx, nginxInstall.Name) nginxFull.ConfigDir = path.Join(nginxFull.Dir, "conf") nginxFull.ConfigFile = "nginx.conf" nginxFull.SiteDir = path.Join(nginxFull.Dir, "www") @@ -47,7 +47,7 @@ func getNginxFull(website *model.Website) (dto.NginxFull, error) { nginxFull.Website = *website var siteNginxConfig dto.NginxConfig nginxFileName := website.Alias + ".conf" - siteConfigPath := path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name, "conf", "conf.d", nginxFileName) + siteConfigPath := path.Join(constant.AppInstallDir, constant.AppNginx, nginxInstall.Name, "conf", "conf.d", nginxFileName) siteNginxConfig.FilePath = siteConfigPath siteNginxContent, err := os.ReadFile(siteConfigPath) if err != nil { diff --git a/backend/app/service/website.go b/backend/app/service/website.go index dcff30c9a..34699f04f 100644 --- a/backend/app/service/website.go +++ b/backend/app/service/website.go @@ -388,7 +388,7 @@ func (w WebsiteService) GetWebsiteNginxConfig(websiteId uint) (dto.FileInfo, err return dto.FileInfo{}, err } - nginxApp, err := appRepo.GetFirst(appRepo.WithKey("nginx")) + nginxApp, err := appRepo.GetFirst(appRepo.WithKey(constant.AppNginx)) if err != nil { return dto.FileInfo{}, err } @@ -397,7 +397,7 @@ func (w WebsiteService) GetWebsiteNginxConfig(websiteId uint) (dto.FileInfo, err return dto.FileInfo{}, err } - configPath := path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name, "conf", "conf.d", website.Alias+".conf") + configPath := path.Join(constant.AppInstallDir, constant.AppNginx, nginxInstall.Name, "conf", "conf.d", website.Alias+".conf") info, err := files.NewFileInfo(files.FileOption{ Path: configPath, @@ -524,7 +524,7 @@ func (w WebsiteService) PreInstallCheck(req request.WebsiteInstallCheckReq) ([]r showErr = false ) - app, err := appRepo.GetFirst(appRepo.WithKey("nginx")) + app, err := appRepo.GetFirst(appRepo.WithKey(constant.AppNginx)) if err != nil { return nil, err } diff --git a/backend/app/service/website_ssl.go b/backend/app/service/website_ssl.go index a35cc7a23..f8b120a23 100644 --- a/backend/app/service/website_ssl.go +++ b/backend/app/service/website_ssl.go @@ -67,11 +67,11 @@ func (w WebsiteSSLService) Create(create dto.WebsiteSSLCreate) (dto.WebsiteSSLCr return res, err } case dto.Http: - appInstall, err := getAppInstallByKey("nginx") + appInstall, err := getAppInstallByKey(constant.AppNginx) if err != nil { return dto.WebsiteSSLCreate{}, err } - if err := client.UseHTTP(path.Join(constant.AppInstallDir, "nginx", appInstall.Name, "root")); err != nil { + if err := client.UseHTTP(path.Join(constant.AppInstallDir, constant.AppNginx, appInstall.Name, "root")); err != nil { return res, err } case dto.DnsManual: @@ -138,11 +138,11 @@ func (w WebsiteSSLService) Renew(sslId uint) error { return err } case dto.Http: - appInstall, err := getAppInstallByKey("nginx") + appInstall, err := getAppInstallByKey(constant.AppNginx) if err != nil { return err } - if err := client.UseHTTP(path.Join(constant.AppInstallDir, "nginx", appInstall.Name, "root")); err != nil { + if err := client.UseHTTP(path.Join(constant.AppInstallDir, constant.AppNginx, appInstall.Name, "root")); err != nil { return err } case dto.DnsManual: diff --git a/backend/app/service/website_utils.go b/backend/app/service/website_utils.go index 7b096199b..ce020cf12 100644 --- a/backend/app/service/website_utils.go +++ b/backend/app/service/website_utils.go @@ -48,12 +48,12 @@ func getDomain(domainStr string, websiteID uint) (model.WebsiteDomain, error) { } func createStaticHtml(website *model.Website) error { - nginxInstall, err := getAppInstallByKey("nginx") + nginxInstall, err := getAppInstallByKey(constant.AppNginx) if err != nil { return err } - indexFolder := path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name, "www", "sites", website.Alias) + indexFolder := path.Join(constant.AppInstallDir, constant.AppNginx, nginxInstall.Name, "www", "sites", website.Alias) indexPath := path.Join(indexFolder, "index.html") indexContent := string(nginx_conf.Index) fileOp := files.NewFileOp() @@ -74,7 +74,7 @@ func createStaticHtml(website *model.Website) error { } func createWebsiteFolder(nginxInstall model.AppInstall, website *model.Website) error { - nginxFolder := path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name) + nginxFolder := path.Join(constant.AppInstallDir, constant.AppNginx, nginxInstall.Name) siteFolder := path.Join(nginxFolder, "www", "sites", website.Alias) fileOp := files.NewFileOp() if !fileOp.Stat(siteFolder) { @@ -98,7 +98,7 @@ func createWebsiteFolder(nginxInstall model.AppInstall, website *model.Website) } func configDefaultNginx(website *model.Website, domains []model.WebsiteDomain) error { - nginxInstall, err := getAppInstallByKey("nginx") + nginxInstall, err := getAppInstallByKey(constant.AppNginx) if err != nil { return err } @@ -107,7 +107,7 @@ func configDefaultNginx(website *model.Website, domains []model.WebsiteDomain) e } nginxFileName := website.Alias + ".conf" - configPath := path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name, "conf", "conf.d", nginxFileName) + configPath := path.Join(constant.AppInstallDir, constant.AppNginx, nginxInstall.Name, "conf", "conf.d", nginxFileName) nginxContent := string(nginx_conf.WebsiteDefault) config := parser.NewStringParser(nginxContent).Parse() servers := config.FindServers() @@ -156,7 +156,7 @@ func configDefaultNginx(website *model.Website, domains []model.WebsiteDomain) e } func delNginxConfig(website model.Website, force bool) error { - nginxApp, err := appRepo.GetFirst(appRepo.WithKey("nginx")) + nginxApp, err := appRepo.GetFirst(appRepo.WithKey(constant.AppNginx)) if err != nil { return err } @@ -169,7 +169,7 @@ func delNginxConfig(website model.Website, force bool) error { } nginxFileName := website.Alias + ".conf" - configPath := path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name, "conf", "conf.d", nginxFileName) + configPath := path.Join(constant.AppInstallDir, constant.AppNginx, nginxInstall.Name, "conf", "conf.d", nginxFileName) fileOp := files.NewFileOp() if !fileOp.Stat(configPath) { @@ -178,7 +178,7 @@ func delNginxConfig(website model.Website, force bool) error { if err := fileOp.DeleteFile(configPath); err != nil { return err } - sitePath := path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name, "www", "sites", website.PrimaryDomain) + sitePath := path.Join(constant.AppInstallDir, constant.AppNginx, nginxInstall.Name, "www", "sites", website.PrimaryDomain) if fileOp.Stat(sitePath) { _ = fileOp.DeleteDir(sitePath) } @@ -247,7 +247,7 @@ func getKeysFromStaticFile(scope dto.NginxKey) []string { } func createPemFile(website model.Website, websiteSSL model.WebsiteSSL) error { - nginxApp, err := appRepo.GetFirst(appRepo.WithKey("nginx")) + nginxApp, err := appRepo.GetFirst(appRepo.WithKey(constant.AppNginx)) if err != nil { return err } @@ -256,7 +256,7 @@ func createPemFile(website model.Website, websiteSSL model.WebsiteSSL) error { return err } - configDir := path.Join(constant.AppInstallDir, "nginx", nginxInstall.Name, "www", "sites", website.Alias, "ssl") + configDir := path.Join(constant.AppInstallDir, constant.AppNginx, nginxInstall.Name, "www", "sites", website.Alias, "ssl") fileOp := files.NewFileOp() if !fileOp.Stat(configDir) { @@ -397,7 +397,7 @@ func handleWebsiteBackup(backupType, baseDir, backupDir, domain, backupName stri return err } - nginxInfo, err := appInstallRepo.LoadBaseInfoByKey("nginx") + nginxInfo, err := appInstallRepo.LoadBaseInfoByKey(constant.AppNginx) if err != nil { return err } @@ -450,7 +450,7 @@ func handleWebsiteBackup(backupType, baseDir, backupDir, domain, backupName stri } func handleWebsiteRecover(website *model.Website, fileDir string) error { - nginxInfo, err := appInstallRepo.LoadBaseInfoByKey("nginx") + nginxInfo, err := appInstallRepo.LoadBaseInfoByKey(constant.AppNginx) if err != nil { return err } diff --git a/backend/buserr/errors.go b/backend/buserr/errors.go index 6def4541b..46a3898b6 100644 --- a/backend/buserr/errors.go +++ b/backend/buserr/errors.go @@ -12,7 +12,6 @@ type BusinessError struct { } func (e BusinessError) Error() string { - content := "" if e.Detail != nil { content = i18n.GetErrMsg(e.Msg, map[string]interface{}{"detail": e.Detail}) diff --git a/backend/constant/app.go b/backend/constant/app.go index a401fbbd0..890cf5b93 100644 --- a/backend/constant/app.go +++ b/backend/constant/app.go @@ -11,4 +11,8 @@ const ( AppNormal = "Normal" AppTakeDown = "TakeDown" + + AppNginx = "nginx" + AppMysql = "mysql" + AppRedis = "redis" )