Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions agent/app/api/v2/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,11 @@ func (b *BaseApi) UpdateSetting(c *gin.Context) {
func (b *BaseApi) LoadBaseDir(c *gin.Context) {
helper.SuccessWithData(c, global.CONF.System.DataDir)
}

func (b *BaseApi) ReloadConn(c *gin.Context) {
if err := settingService.ReloadConn(); err != nil {
helper.InternalServer(c, err)
return
}
helper.SuccessWithOutData(c)
}
55 changes: 55 additions & 0 deletions agent/app/service/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ import (
"time"

"github.com/1Panel-dev/1Panel/agent/app/dto"
"github.com/1Panel-dev/1Panel/agent/app/repo"
"github.com/1Panel-dev/1Panel/agent/constant"
"github.com/1Panel-dev/1Panel/agent/global"
"github.com/1Panel-dev/1Panel/agent/utils/encrypt"
"github.com/1Panel-dev/1Panel/agent/utils/xpack"
)

type SettingService struct{}

type ISettingService interface {
GetSettingInfo() (*dto.SettingInfo, error)
Update(key, value string) error

ReloadConn() error
}

func NewISettingService() ISettingService {
Expand Down Expand Up @@ -59,3 +65,52 @@ func (u *SettingService) Update(key, value string) error {
}
return nil
}

func (u *SettingService) ReloadConn() error {
if global.IsMaster {
return nil
}
isLocal, nodeInfo, err := xpack.LoadNodeInfo()
if err != nil {
global.LOG.Errorf("load new node info failed, err: %v", err)
return nil
}
if isLocal {
return nil
}

settingRepo := repo.NewISettingRepo()
itemKey, _ := encrypt.StringEncrypt(nodeInfo.ServerKey)
if err := settingRepo.Update("ServerKey", itemKey); err != nil {
global.LOG.Errorf("update server key failed, err: %v", err)
return nil
}
itemCrt, _ := encrypt.StringEncrypt(nodeInfo.ServerCrt)
if err := settingRepo.Update("ServerCrt", itemCrt); err != nil {
global.LOG.Errorf("update server crt failed, err: %v", err)
return nil
}
if err := settingRepo.Update("CurrentNode", nodeInfo.CurrentNode); err != nil {
global.LOG.Errorf("update current node failed, err: %v", err)
return nil
}
if err := settingRepo.Update("SystemVersion", nodeInfo.Version); err != nil {
global.LOG.Errorf("update system version failed, err: %v", err)
return nil
}
if err := settingRepo.Update("BaseDir", nodeInfo.BaseDir); err != nil {
global.LOG.Errorf("update base dir failed, err: %v", err)
return nil
}
if err := settingRepo.Update("MasterAddr", nodeInfo.MasterAddr); err != nil {
global.LOG.Errorf("update master addr failed, err: %v", err)
return nil
}

global.CONF.System.BaseDir, _ = settingRepo.GetValueByKey("BaseDir")
global.CONF.System.Version, _ = settingRepo.GetValueByKey("SystemVersion")
global.CONF.System.EncryptKey, _ = settingRepo.GetValueByKey("EncryptKey")
global.CONF.System.CurrentNode, _ = settingRepo.GetValueByKey("CurrentNode")
global.CONF.System.MasterAddr, _ = settingRepo.GetValueByKey("MasterAddr")
return nil
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that there are several issues with this code snippet.

Issues

  1. Type mismatches

    • The first class definition (SettingService) does not correctly extend from the interface ISettingService.
  2. Interface methods and implementation of update method missing

    • It is important to implement all the methods specified in the interface, otherwise it will lead to undefined behavior.

Suggestions

I would make these changes:

type SettingService struct{}

// GetSettingInfo retrieves a setting object's information based on its key.
func (s *SettingService) GetSettingInfo(key string) (*App.GetSettingData, error) {
  // Implementation to retrieve an app SettingData
}

// Update updates an existing setting based on provided key-value pairs.
func (s *SettingService) Update(key string, value string) error {
  dataToUpdate, ok := App.GetSettings[key]
  if !ok {
    return fmt.Errorf("no such setting found")
  }

  // TODO: Implement logic to update database

  return nil
}

These implementations should fix the aforementioned bugs and improve readability and maintainability.

Also, don't forget to add docstrings around functions and variable comments. This makes your code more accessible and easier for others who may read / modify your work.

1 change: 0 additions & 1 deletion agent/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ require (
github.com/docker/docker v27.1.0+incompatible
github.com/docker/go-connections v0.5.0
github.com/fsnotify/fsnotify v1.7.0
github.com/gin-contrib/gzip v1.0.1
github.com/gin-gonic/gin v1.10.0
github.com/glebarez/sqlite v1.11.0
github.com/go-acme/lego/v4 v4.17.4
Expand Down
2 changes: 0 additions & 2 deletions agent/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,6 @@ github.com/fvbommel/sortorder v1.0.2 h1:mV4o8B2hKboCdkJm+a7uX/SIpZob4JzUpc5GGnM4
github.com/fvbommel/sortorder v1.0.2/go.mod h1:uk88iVf1ovNn1iLfgUVU2F9o5eO30ui720w+kxuqRs0=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/gin-contrib/gzip v1.0.1 h1:HQ8ENHODeLY7a4g1Au/46Z92bdGFl74OhxcZble9WJE=
github.com/gin-contrib/gzip v1.0.1/go.mod h1:njt428fdUNRvjuJf16tZMYZ2Yl+WQB53X5wmhDwXvC4=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
Expand Down
2 changes: 1 addition & 1 deletion agent/init/hook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func Init() {
initWithNodeJson()
service.NewISettingService().ReloadConn()
initGlobalData()
handleCronjobStatus()
handleSnapStatus()
Expand Down
1 change: 1 addition & 0 deletions agent/router/ro_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func (s *SettingRouter) InitRouter(Router *gin.RouterGroup) {
settingRouter.POST("/search", baseApi.GetSettingInfo)
settingRouter.GET("/search/available", baseApi.GetSystemAvailable)
settingRouter.POST("/update", baseApi.UpdateSetting)
settingRouter.POST("/conn/reload", baseApi.ReloadConn)

settingRouter.GET("/snapshot/load", baseApi.LoadSnapshotData)
settingRouter.POST("/snapshot", baseApi.CreateSnapshot)
Expand Down
30 changes: 15 additions & 15 deletions core/app/service/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (u *SettingService) UpdatePort(port uint) error {
if common.ScanPort(int(port)) {
return buserr.WithDetail(constant.ErrPortInUsed, port, nil)
}
oldPort, err := settingRepo.Get(repo.WithByKey("Port"))
oldPort, err := settingRepo.Get(repo.WithByKey("ServerPort"))
if err != nil {
return err
}
Expand All @@ -197,6 +197,20 @@ func (u *SettingService) UpdatePort(port uint) error {
if err := firewall.UpdatePort(oldPort.Value, fmt.Sprintf("%v", port)); err != nil {
return err
}
masterAddr, err := settingRepo.Get(repo.WithByKey("MasterAddr"))
if err != nil {
global.LOG.Errorf("load master addr from db failed, err: %v", err)
return err
}
if len(masterAddr.Value) != 0 {
oldMasterPort := loadPort(masterAddr.Value)
if len(oldMasterPort) != 0 {
if err := xpack.UpdateMasterAddr(strings.ReplaceAll(masterAddr.Value, oldMasterPort, fmt.Sprintf("%v", port))); err != nil {
global.LOG.Errorf("update master addr from db failed, err: %v", err)
return err
}
}
}

if err := settingRepo.Update("ServerPort", strconv.Itoa(int(port))); err != nil {
return err
Expand All @@ -208,20 +222,6 @@ func (u *SettingService) UpdatePort(port uint) error {
global.LOG.Errorf("restart system port failed, err: %v", err)
}
}()
masterAddr, err := settingRepo.Get(repo.WithByKey("MasterAddr"))
if err != nil {
global.LOG.Errorf("load master addr from db failed, err: %v", err)
return
}
if len(masterAddr.Value) != 0 {
oldMasterPort := loadPort(masterAddr.Value)
if len(oldMasterPort) != 0 {
if err := xpack.UpdateMasterAddr(strings.ReplaceAll(masterAddr.Value, oldMasterPort, fmt.Sprintf("%v", port))); err != nil {
global.LOG.Errorf("update master addr from db failed, err: %v", err)
return
}
}
}
}()
return nil
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There does not appear to be any obvious issues with this code snippet from an English perspective. It appears it checks if a provided port number is valid using a scan function and updates settings accordingly through settingRepo. However, since it only mentions reloading database information for updating the port, more context would provide more specific guidance on improvements.

Expand Down
1 change: 0 additions & 1 deletion core/constant/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const (
StatusWaiting = "Waiting"
StatusPacking = "Packing"
StatusSending = "Sending"
StatusChecking = "Checking"
StatusStarting = "Starting"
StatusHealthy = "Healthy"
StatusUnhealthy = "Unhealthy"
Expand Down
69 changes: 69 additions & 0 deletions frontend/src/components/dialog-pro/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<template>
<el-dialog
:title="title"
v-model="dialogVisible"
:destroy-on-close="true"
:close-on-click-modal="false"
:width="size"
>
<div v-if="slots.content">
<slot name="content"></slot>
</div>
<el-row v-else>
<el-col :span="22" :offset="1">
<slot></slot>
</el-col>
</el-row>

<template #footer v-if="slots.footer">
<slot name="footer"></slot>
</template>
</el-dialog>
</template>

<script lang="ts" setup>
import { computed, useSlots } from 'vue';
defineOptions({ name: 'DrawerPro' });

const props = defineProps({
title: String,
size: {
type: String,
default: 'normal',
},
modelValue: {
type: Boolean,
default: false,
},
});

const slots = useSlots();

const emit = defineEmits(['update:modelValue', 'close']);

const size = computed(() => {
switch (props.size) {
case 'small':
return '30%';
case 'normal':
return '40%';
case 'large':
return '50%';
case 'full':
return '100%';
case '60%':
return '60%';
default:
return '50%';
}
});

const dialogVisible = computed({
get() {
return props.modelValue;
},
set(value: boolean) {
emit('update:modelValue', value);
},
});
</script>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the provided snippet, here are some observations and suggested improvements:

  1. Size Property Naming: size property is redundant since it's already stored as an instance variable named size.
  2. Slots Implementation: The <template> tags do not match their <slot> directives, specifically because <template> elements should be placed inside the <div> element for inline slot processing instead of placing them outside <slot>s.
  3. Dialog Width Management: Use $attrs to access the width directly when creating this component rather than setting via .width="size".

Here's the refined code based on these points:

<div class="drawer-pro" @click="$emit('close')">
    <div :class="'drawer-' ~ $attrs.width || "normal"' ref="drawer"> <!-- Adjusted line -->
      <!-- Content goes here -->
    </div> 
</div>

This version addresses most concerns listed above while maintaining a consistent syntax across all components within this package if it were included laterally, like in el-container.

2 changes: 2 additions & 0 deletions frontend/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import CopyButton from '@/components/copy-button/index.vue';
import MsgInfo from '@/components/msg-info/index.vue';
import MainDiv from '@/components/main-div/index.vue';
import DrawerPro from '@/components/drawer-pro/index.vue';
import DialogPro from '@/components/dialog-pro/index.vue';
export default {
install(app: App) {
app.component(LayoutContent.name, LayoutContent);
Expand All @@ -27,5 +28,6 @@ export default {
app.component(MsgInfo.name, MsgInfo);
app.component(MainDiv.name, MainDiv);
app.component(DrawerPro.name, DrawerPro);
app.component(DialogPro.name, DialogPro);
},
};
Loading