feat: 修改应用端口,同时修改网站配置

This commit is contained in:
zhengkunwang223 2023-03-08 23:58:42 +08:00 committed by zhengkunwang223
parent 25149bbede
commit 3bd20e5b28
5 changed files with 49 additions and 9 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -25,6 +25,7 @@ ErrNotInstall: "应用未安装"
ErrPortInOtherApp: "{{ .port }} 端口已被 {{ .apps }}占用!"
ErrDbUserNotValid: "存量数据库,用户名密码不匹配!"
ErrDockerComposeNotValid: "docker-compose 文件格式错误"
ErrUpdateBuWebsite: '应用更新成功,但是网站配置文件修改失败,请检查配置!'
#file
ErrFileCanNotRead: "此文件不支持预览"