mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-09-08 10:14:35 +08:00
Go CLI: refactor (#16355)
This commit is contained in:
+121
-2
@@ -20,7 +20,10 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"ragflow/internal/common"
|
||||
"ragflow/internal/engine/redis"
|
||||
"ragflow/internal/entity"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -32,11 +35,15 @@ import (
|
||||
)
|
||||
|
||||
// SystemService system service
|
||||
type SystemService struct{}
|
||||
type SystemService struct {
|
||||
systemSettingsDAO *dao.SystemSettingsDAO
|
||||
}
|
||||
|
||||
// NewSystemService create system service
|
||||
func NewSystemService() *SystemService {
|
||||
return &SystemService{}
|
||||
return &SystemService{
|
||||
systemSettingsDAO: dao.NewSystemSettingsDAO(),
|
||||
}
|
||||
}
|
||||
|
||||
// ConfigResponse system configuration response
|
||||
@@ -370,3 +377,115 @@ func (s *SystemService) Healthz(ctx context.Context) (*HealthzResponse, bool) {
|
||||
}
|
||||
return result, allOK
|
||||
}
|
||||
|
||||
// ListAllVariables list all variables
|
||||
// Returns all system settings from database
|
||||
func (s *SystemService) ListAllVariables() ([]map[string]interface{}, error) {
|
||||
settings, err := s.systemSettingsDAO.GetAll()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return common.FormatSystemSettings(settings), nil
|
||||
}
|
||||
|
||||
// SetVariable set variable
|
||||
// Creates or updates a system setting
|
||||
// If the setting exists, updates it; otherwise creates a new one
|
||||
func (s *SystemService) SetVariable(varName, varValue string) error {
|
||||
settings, err := s.systemSettingsDAO.GetByName(varName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(settings) == 1 {
|
||||
setting := &settings[0]
|
||||
if err = common.ValidateSystemSettingValue(*setting, varValue); err != nil {
|
||||
return err
|
||||
}
|
||||
setting.Value = varValue
|
||||
return s.systemSettingsDAO.UpdateByName(varName, setting)
|
||||
} else if len(settings) > 1 {
|
||||
return fmt.Errorf("can't update more than 1 setting: %s", varName)
|
||||
}
|
||||
|
||||
dataType := common.InferSystemSettingDataType(varName)
|
||||
newSetting := &entity.SystemSettings{
|
||||
Name: varName,
|
||||
Value: varValue,
|
||||
Source: "admin",
|
||||
DataType: dataType,
|
||||
}
|
||||
if err = common.ValidateSystemSettingValue(*newSetting, varValue); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.systemSettingsDAO.Create(newSetting)
|
||||
}
|
||||
|
||||
// Config methods
|
||||
|
||||
// ListAllConfigs list all configs
|
||||
// Returns all service configurations from the config file
|
||||
func (s *SystemService) ListAllConfigs() ([]map[string]interface{}, error) {
|
||||
result := server.GetAllConfigs()
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// Environment methods
|
||||
|
||||
// ListEnvironments list all environments
|
||||
func (s *SystemService) ListEnvironments() ([]map[string]interface{}, error) {
|
||||
result := make([]map[string]interface{}, 0)
|
||||
|
||||
// DOC_ENGINE
|
||||
docEngine := os.Getenv("DOC_ENGINE")
|
||||
if docEngine == "" {
|
||||
docEngine = "elasticsearch"
|
||||
}
|
||||
result = append(result, map[string]interface{}{
|
||||
"env": "DOC_ENGINE",
|
||||
"value": docEngine,
|
||||
})
|
||||
|
||||
// DEFAULT_SUPERUSER_EMAIL
|
||||
defaultSuperuserEmail := os.Getenv("DEFAULT_SUPERUSER_EMAIL")
|
||||
if defaultSuperuserEmail == "" {
|
||||
defaultSuperuserEmail = "admin@ragflow.io"
|
||||
}
|
||||
result = append(result, map[string]interface{}{
|
||||
"env": "DEFAULT_SUPERUSER_EMAIL",
|
||||
"value": defaultSuperuserEmail,
|
||||
})
|
||||
|
||||
// DB_TYPE
|
||||
dbType := os.Getenv("DB_TYPE")
|
||||
if dbType == "" {
|
||||
dbType = "mysql"
|
||||
}
|
||||
result = append(result, map[string]interface{}{
|
||||
"env": "DB_TYPE",
|
||||
"value": dbType,
|
||||
})
|
||||
|
||||
// DEVICE
|
||||
device := os.Getenv("DEVICE")
|
||||
if device == "" {
|
||||
device = "cpu"
|
||||
}
|
||||
result = append(result, map[string]interface{}{
|
||||
"env": "DEVICE",
|
||||
"value": device,
|
||||
})
|
||||
|
||||
// STORAGE_IMPL
|
||||
storageImpl := os.Getenv("STORAGE_IMPL")
|
||||
if storageImpl == "" {
|
||||
storageImpl = "MINIO"
|
||||
}
|
||||
result = append(result, map[string]interface{}{
|
||||
"env": "STORAGE_IMPL",
|
||||
"value": storageImpl,
|
||||
})
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user