diff --git a/backend/app/service/app_install.go b/backend/app/service/app_install.go index e02372c79..3759ed519 100644 --- a/backend/app/service/app_install.go +++ b/backend/app/service/app_install.go @@ -3,6 +3,7 @@ package service import ( "encoding/json" "fmt" + "github.com/1Panel-dev/1Panel/backend/utils/nginx" "github.com/joho/godotenv" "io/ioutil" "math" @@ -199,10 +200,12 @@ func (a AppInstallService) Update(req request.AppInstalledUpdate) error { if err != nil { return err } + changePort := false port, ok := req.Params["PANEL_APP_PORT_HTTP"] if ok { portN := int(math.Ceil(port.(float64))) if portN != installed.HttpPort { + changePort = true httpPort, err := checkPort("PANEL_APP_PORT_HTTP", req.Params) if err != nil { return err @@ -237,7 +240,33 @@ func (a AppInstallService) Update(req request.AppInstalledUpdate) error { return err } _ = appInstallRepo.Save(&installed) - return rebuildApp(installed) + + if err := rebuildApp(installed); err != nil { + return err + } + website, _ := websiteRepo.GetFirst(websiteRepo.WithAppInstallId(installed.ID)) + if changePort && website.ID != 0 && website.Status == constant.Running { + nginxInstall, err := getNginxFull(&website) + if err != nil { + return buserr.WithErr(constant.ErrUpdateBuWebsite, err) + } + config := nginxInstall.SiteConfig.Config + servers := config.FindServers() + if len(servers) == 0 { + return buserr.WithErr(constant.ErrUpdateBuWebsite, errors.New("nginx config is not valid")) + } + server := servers[0] + proxy := fmt.Sprintf("http://127.0.0.1:%d", installed.HttpPort) + server.UpdateRootProxy([]string{proxy}) + + if err := nginx.WriteConfig(config, nginx.IndentedStyle); err != nil { + return buserr.WithErr(constant.ErrUpdateBuWebsite, err) + } + if err := nginxCheckAndReload(nginxInstall.SiteConfig.OldContent, config.FilePath, nginxInstall.Install.ContainerName); err != nil { + return buserr.WithErr(constant.ErrUpdateBuWebsite, err) + } + } + return nil } func (a AppInstallService) SyncAll() error { diff --git a/backend/buserr/errors.go b/backend/buserr/errors.go index 43b843ec4..67a57dc1b 100644 --- a/backend/buserr/errors.go +++ b/backend/buserr/errors.go @@ -46,6 +46,14 @@ func WithDetail(Key string, detail interface{}, err error) BusinessError { } } +func WithErr(Key string, err error) BusinessError { + return BusinessError{ + Msg: Key, + Detail: "", + Err: err, + } +} + func WithMap(Key string, maps map[string]interface{}, err error) BusinessError { return BusinessError{ Msg: Key, diff --git a/backend/constant/errs.go b/backend/constant/errs.go index ae3a7ce9b..51e7d0cfb 100644 --- a/backend/constant/errs.go +++ b/backend/constant/errs.go @@ -50,14 +50,15 @@ var ( // app var ( - ErrPortInUsed = "ErrPortInUsed" - ErrAppLimit = "ErrAppLimit" - ErrAppRequired = "ErrAppRequired" - ErrFileCanNotRead = "ErrFileCanNotRead" - ErrFileToLarge = "ErrFileToLarge" - ErrNotInstall = "ErrNotInstall" - ErrPortInOtherApp = "ErrPortInOtherApp" - ErrDbUserNotValid = "ErrDbUserNotValid" + ErrPortInUsed = "ErrPortInUsed" + ErrAppLimit = "ErrAppLimit" + ErrAppRequired = "ErrAppRequired" + ErrFileCanNotRead = "ErrFileCanNotRead" + ErrFileToLarge = "ErrFileToLarge" + ErrNotInstall = "ErrNotInstall" + ErrPortInOtherApp = "ErrPortInOtherApp" + ErrDbUserNotValid = "ErrDbUserNotValid" + ErrUpdateBuWebsite = "ErrUpdateBuWebsite" ) //website diff --git a/backend/i18n/lang/en.yaml b/backend/i18n/lang/en.yaml index 2a609a7ec..62e3ad2be 100644 --- a/backend/i18n/lang/en.yaml +++ b/backend/i18n/lang/en.yaml @@ -25,6 +25,7 @@ ErrNotInstall: "App not installed" ErrPortInOtherApp: "{{ .port }} port already in use by {{ .apps }}" ErrDbUserNotValid: "Stock database, username and password do not match!" ErrDockerComposeNotValid: "docker-compose file format error!" +ErrUpdateBuWebsite: 'The application was updated successfully, but the modification of the website configuration file failed, please check the configuration!' #file ErrFileCanNotRead: "File can not read" diff --git a/backend/i18n/lang/zh.yaml b/backend/i18n/lang/zh.yaml index 721d2db89..f69712ace 100644 --- a/backend/i18n/lang/zh.yaml +++ b/backend/i18n/lang/zh.yaml @@ -25,6 +25,7 @@ ErrNotInstall: "应用未安装" ErrPortInOtherApp: "{{ .port }} 端口已被 {{ .apps }}占用!" ErrDbUserNotValid: "存量数据库,用户名密码不匹配!" ErrDockerComposeNotValid: "docker-compose 文件格式错误" +ErrUpdateBuWebsite: '应用更新成功,但是网站配置文件修改失败,请检查配置!' #file ErrFileCanNotRead: "此文件不支持预览"