Go CLI: refactor (#16355)

This commit is contained in:
Jin Hai
2026-06-25 20:36:50 +08:00
committed by GitHub
parent 304d9e02bb
commit dbefadd86a
15 changed files with 768 additions and 180 deletions

View File

@@ -957,7 +957,7 @@ func (h *Handler) HandleNoRoute(c *gin.Context) {
// GetLogLevel returns the current log level
func (h *Handler) GetLogLevel(c *gin.Context) {
level := common.GetLevel()
success(c, gin.H{"level": level}, "")
success(c, gin.H{"level": level}, "SUCCESS")
}
// SetLogLevelRequest set log level request
@@ -978,7 +978,7 @@ func (h *Handler) SetLogLevel(c *gin.Context) {
return
}
success(c, gin.H{"level": req.Level}, "Log level updated successfully")
success(c, gin.H{"level": req.Level}, "SUCCESS")
}
func (h *Handler) ListMessagesFromQueue(c *gin.Context) {

View File

@@ -83,6 +83,9 @@ func (r *Router) Setup(engine *gin.Engine) {
// Configs
protected.GET("/configs", r.handler.ListConfigs)
// Log level
protected.GET("/config/log", r.handler.GetLogLevel)
protected.PUT("/config/log", r.handler.SetLogLevel)
// Environments
protected.GET("/environments", r.handler.ListEnvironments)
@@ -90,10 +93,6 @@ func (r *Router) Setup(engine *gin.Engine) {
// Version
protected.GET("/version", r.handler.GetVersion)
// Log level
protected.GET("/log_level", r.handler.GetLogLevel)
protected.PUT("/log_level", r.handler.SetLogLevel)
queue := protected.Group("/queue")
{
queue.GET("/", r.handler.ShowMessageQueue)

View File

@@ -21,7 +21,6 @@ import (
"crypto/tls"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"net/http"
@@ -36,7 +35,6 @@ import (
"ragflow/internal/utility"
"regexp"
"strconv"
"strings"
"time"
"go.uber.org/zap"
@@ -1456,56 +1454,6 @@ func NewAdminException(message string) *AdminException {
}
}
func formatSystemSetting(setting entity.SystemSettings) map[string]interface{} {
return map[string]interface{}{
"data_type": setting.DataType,
"name": setting.Name,
"setting_type": "config",
"value": setting.Value,
}
}
func formatSystemSettings(settings []entity.SystemSettings) []map[string]interface{} {
result := make([]map[string]interface{}, 0, len(settings))
for _, setting := range settings {
result = append(result, formatSystemSetting(setting))
}
return result
}
func validateSystemSettingValue(setting entity.SystemSettings, value string) error {
dataType := strings.ToLower(setting.DataType)
switch dataType {
case "string":
return nil
case "integer", "int":
if _, err := strconv.Atoi(value); err != nil {
return NewAdminException(fmt.Sprintf("Invalid integer value for %s: %s", setting.Name, value))
}
case "bool", "boolean":
if value != "true" && value != "false" {
return NewAdminException(fmt.Sprintf("Invalid bool value for %s: expected true or false", setting.Name))
}
case "json":
if !json.Valid([]byte(value)) {
return NewAdminException(fmt.Sprintf("Invalid JSON value for %s", setting.Name))
}
default:
return NewAdminException(fmt.Sprintf("Unsupported data type for %s: %s", setting.Name, setting.DataType))
}
return nil
}
func inferSystemSettingDataType(name string) string {
if strings.HasPrefix(name, "sandbox.") {
return "json"
}
if strings.HasSuffix(name, ".enabled") {
return "bool"
}
return "string"
}
// GetVariable get variable by name
// Returns the exact system setting with the given name, or settings matching the
// given name prefix when an exact setting does not exist.
@@ -1524,7 +1472,7 @@ func (s *Service) GetVariable(varName string) ([]map[string]interface{}, error)
return nil, NewAdminException("Can't get setting: " + varName)
}
}
return formatSystemSettings(settings), nil
return common.FormatSystemSettings(settings), nil
}
// ListAllVariables list all variables
@@ -1535,7 +1483,7 @@ func (s *Service) ListAllVariables() ([]map[string]interface{}, error) {
return nil, err
}
return formatSystemSettings(settings), nil
return common.FormatSystemSettings(settings), nil
}
// SetVariable set variable
@@ -1549,7 +1497,7 @@ func (s *Service) SetVariable(varName, varValue string) error {
if len(settings) == 1 {
setting := &settings[0]
if err := validateSystemSettingValue(*setting, varValue); err != nil {
if err = common.ValidateSystemSettingValue(*setting, varValue); err != nil {
return err
}
setting.Value = varValue
@@ -1558,14 +1506,14 @@ func (s *Service) SetVariable(varName, varValue string) error {
return NewAdminException("Can't update more than 1 setting: " + varName)
}
dataType := inferSystemSettingDataType(varName)
dataType := common.InferSystemSettingDataType(varName)
newSetting := &entity.SystemSettings{
Name: varName,
Value: varValue,
Source: "admin",
DataType: dataType,
}
if err := validateSystemSettingValue(*newSetting, varValue); err != nil {
if err = common.ValidateSystemSettingValue(*newSetting, varValue); err != nil {
return err
}
return s.systemSettingsDAO.Create(newSetting)

View File

@@ -17,6 +17,7 @@
package admin
import (
"ragflow/internal/common"
"ragflow/internal/entity"
"testing"
)
@@ -42,7 +43,7 @@ func TestValidateSystemSettingValue(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
setting := entity.SystemSettings{Name: "test.setting", DataType: tt.dataType}
err := validateSystemSettingValue(setting, tt.value)
err := common.ValidateSystemSettingValue(setting, tt.value)
if (err != nil) != tt.wantError {
t.Fatalf("validateSystemSettingValue() error = %v, wantError %v", err, tt.wantError)
}
@@ -58,7 +59,7 @@ func TestInferSystemSettingDataType(t *testing.T) {
}
for name, want := range tests {
if got := inferSystemSettingDataType(name); got != want {
if got := common.InferSystemSettingDataType(name); got != want {
t.Fatalf("inferSystemSettingDataType(%q) = %q, want %q", name, got, want)
}
}