Skip to content

Commit

Permalink
feat: Support modifying the node port (#7576)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssongliu authored Dec 28, 2024
1 parent 4d548ad commit 72c86c3
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 3 deletions.
1 change: 1 addition & 0 deletions agent/app/model/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Setting struct {
type NodeInfo struct {
Scope string `json:"scope"`
BaseDir string `json:"baseDir"`
NodePort uint `json:"nodePort"`
Version string `json:"version"`
ServerCrt string `json:"serverCrt"`
ServerKey string `json:"serverKey"`
Expand Down
6 changes: 6 additions & 0 deletions agent/app/service/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"encoding/json"
"fmt"
"time"

"github.com/1Panel-dev/1Panel/agent/app/dto"
Expand Down Expand Up @@ -103,9 +104,14 @@ func (u *SettingService) ReloadConn() error {
global.LOG.Errorf("update base dir failed, err: %v", err)
return nil
}
if err := settingRepo.Update("NodePort", fmt.Sprintf("%v", nodeInfo.NodePort)); err != nil {
global.LOG.Errorf("update node port failed, err: %v", err)
return nil
}

global.CONF.System.BaseDir = nodeInfo.BaseDir
global.CONF.System.Version = nodeInfo.Version
global.CONF.System.Port = fmt.Sprintf("%v", nodeInfo.NodePort)
global.IsMaster = nodeInfo.Scope == "master"
return nil
}
1 change: 1 addition & 0 deletions agent/configs/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package configs
type System struct {
Mode string `mapstructure:"mode"`

Port string `mapstructure:"version"`
Version string `mapstructure:"version"`
BaseDir string `mapstructure:"base_dir"`
EncryptKey string `mapstructure:"encrypt_key"`
Expand Down
1 change: 1 addition & 0 deletions agent/init/migration/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func InitAgentDB() {
migrations.InitImageRepo,
migrations.InitDefaultCA,
migrations.InitPHPExtensions,
migrations.InitNodePort,
})
if err := m.Migrate(); err != nil {
global.LOG.Error(err)
Expand Down
19 changes: 19 additions & 0 deletions agent/init/migration/migrations/init.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package migrations

import (
"fmt"

"github.com/1Panel-dev/1Panel/agent/app/dto/request"
"github.com/1Panel-dev/1Panel/agent/app/model"
"github.com/1Panel-dev/1Panel/agent/app/service"
Expand Down Expand Up @@ -92,6 +94,9 @@ var InitSetting = &gormigrate.Migration{
if err := tx.Create(&model.Setting{Key: "NodeScope", Value: nodeInfo.Scope}).Error; err != nil {
return err
}
if err := tx.Create(&model.Setting{Key: "NodePort", Value: fmt.Sprintf("%v", nodeInfo.NodePort)}).Error; err != nil {
return err
}
if err := tx.Create(&model.Setting{Key: "SystemVersion", Value: nodeInfo.Version}).Error; err != nil {
return err
}
Expand Down Expand Up @@ -226,3 +231,17 @@ var AddTaskTable = &gormigrate.Migration{
)
},
}

var InitNodePort = &gormigrate.Migration{
ID: "20241226-init-node-port",
Migrate: func(tx *gorm.DB) error {
var itemPort model.Setting
_ = tx.Where("key = ?", "NodePort").First(&itemPort).Error
if itemPort.ID == 0 {
if err := tx.Create(&model.Setting{Key: "NodePort", Value: "9999"}).Error; err != nil {
return err
}
}
return nil
},
}
7 changes: 4 additions & 3 deletions agent/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package server
import (
"crypto/tls"
"fmt"
"github.com/1Panel-dev/1Panel/agent/constant"
"net"
"net/http"
"os"

"github.com/1Panel-dev/1Panel/agent/constant"

"github.com/1Panel-dev/1Panel/agent/app/repo"
"github.com/1Panel-dev/1Panel/agent/cron"
"github.com/1Panel-dev/1Panel/agent/global"
Expand Down Expand Up @@ -64,7 +65,7 @@ func Start() {
_ = server.Serve(listener)
return
} else {
server.Addr = "0.0.0.0:9999"
server.Addr = fmt.Sprintf("0.0.0.0:%s", global.CONF.System.Port)
settingRepo := repo.NewISettingRepo()
certItem, err := settingRepo.Get(settingRepo.WithByKey("ServerCrt"))
if err != nil {
Expand All @@ -86,7 +87,7 @@ func Start() {
ClientAuth: tls.RequireAnyClientCert,
}
business.Init()
global.LOG.Info("listen at https://0.0.0.0:9999")
global.LOG.Infof("listen at https://0.0.0.0:%s", global.CONF.System.Port)
if err := server.ListenAndServeTLS("", ""); err != nil {
panic(err)
}
Expand Down

0 comments on commit 72c86c3

Please sign in to comment.