Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

Go: refactor (#17511)

Jin Hai1f90755c4822
diff --git a/cmd/ragflow_server.go b/cmd/ragflow_server.go
index 15f7698640..944c63ed16 100644
--- a/cmd/ragflow_server.go
+++ b/cmd/ragflow_server.go
@@ -220,24 +220,24 @@ func main() {
arguments, err := parseArgs()
if err != nil {
fmt.Printf("Failed to parse arguments: %v\n", err)
- return
+ os.Exit(1)
}
if arguments.helpFlag || arguments.mode == nil {
printHelp(arguments)
- return
+ os.Exit(1)
}
if arguments.versionFlag {
fmt.Printf("RAGFlow version: %s\n", common.GetRAGFlowVersion())
- return
+ os.Exit(1)
}
// Initialize local variables (runtime variables from Redis)
err = server.InitLocalVariables()
if err != nil {
fmt.Printf("Failed to start %s server: %v\n", *arguments.mode, err)
- return
+ os.Exit(1)
}
// Temporary logger initialization
@@ -267,7 +267,7 @@ func main() {
if err = server.Init(configPath); err != nil {
common.Error("Failed to initialize configuration", err)
- return
+ os.Exit(1)
}
config := server.GetConfig()
@@ -275,10 +275,10 @@ func main() {
// override default port if provided
switch *arguments.mode {
case "api":
- port := config.Server.Port
+ port := config.APIServer.Port
if arguments.port != nil {
port = *arguments.port
- config.Server.Port = port
+ config.APIServer.Port = port
}
if arguments.name == nil {
serverName = fmt.Sprintf("api_server_%d", port)
@@ -303,8 +303,9 @@ func main() {
serverName = fmt.Sprintf("syncer_server_%s", uuid)
}
default:
- common.Error("invalid server mode", errors.New(*arguments.mode))
- return
+ err = errors.New(*arguments.mode)
+ common.Error("invalid server mode", err)
+ os.Exit(1)
}
// set server name and log file path
@@ -333,6 +334,8 @@ func main() {
if config.Log.Path != "" {
fileOut.Path = config.Log.Path
}
+
+ common.SyncLog()
if err = common.Init(logLevel, fileOut, serverName); err != nil {
common.Error("Failed to reinitialize logger with configured level", err)
}
@@ -340,7 +343,7 @@ func main() {
server.SetLogger(common.Logger)
// Print all configuration settings
- common.Info(fmt.Sprintf("Starting %s server: %s, mode: %s", *arguments.mode, serverName, config.Server.Mode))
+ common.Info(fmt.Sprintf("Starting %s server: %s, mode: %s", *arguments.mode, serverName, config.General.Mode))
server.PrintAll()
// Initialize database
@@ -377,7 +380,7 @@ func main() {
if err = server.StartServer(ctx, cancel, serverName); err != nil {
common.Error("Failed to start EE server", err)
- return
+ os.Exit(1)
}
defer server.ShutdownServer(ctx)
@@ -389,36 +392,36 @@ func main() {
case "api":
if err = runAPI(ctx, arguments, config); err != nil {
fmt.Printf("Failed to start API server: %v\n", err)
- return
+ os.Exit(1)
}
case "admin":
- if err = runAdmin(ctx, cancel, arguments, config); err != nil {
+ if err = runAdmin(ctx, arguments, config); err != nil {
fmt.Printf("Failed to start ADMIN server: %v\n", err)
- return
+ os.Exit(1)
}
case "ingestor":
if err = runIngestor(ctx, cancel, arguments, config); err != nil {
fmt.Printf("Failed to start INGESTION worker: %v\n", err)
- return
+ os.Exit(1)
}
case "syncer":
if err = runSyncer(ctx, cancel, arguments, config); err != nil {
fmt.Printf("Failed to start SYNCER: %v\n", err)
- return
+ os.Exit(1)
}
default:
fmt.Printf("Invalid server mode: %s\n", *arguments.mode)
- return
+ os.Exit(1)
}
}
-func runAdmin(ctx context.Context, cancel context.CancelFunc, args *serverArgs, config *server.Config) error {
+func runAdmin(ctx context.Context, args *serverArgs, config *server.Config) error {
// Set Gin mode
- if config.Server.Mode == "release" {
- gin.SetMode(gin.ReleaseMode)
- } else {
+ if config.General.Mode == "debug" {
gin.SetMode(gin.DebugMode)
+ } else {
+ gin.SetMode(gin.ReleaseMode)
}
adminService := admin.NewService()
@@ -659,10 +662,10 @@ func runAPI(ctx context.Context, args *serverArgs, config *server.Config) error
func startServer(ctx context.Context, config *server.Config) {
// Set Gin mode
- if config.Server.Mode == "release" {
- gin.SetMode(gin.ReleaseMode)
- } else {
+ if config.General.Mode == "debug" {
gin.SetMode(gin.DebugMode)
+ } else {
+ gin.SetMode(gin.ReleaseMode)
}
// Initialize service layer
@@ -844,7 +847,7 @@ func startServer(ctx context.Context, config *server.Config) {
r.Setup(ginEngine)
// Create HTTP server with timeouts to prevent slow clients from blocking shutdown
- addr := fmt.Sprintf(":%d", config.Server.Port)
+ addr := fmt.Sprintf(":%d", config.APIServer.Port)
srv := &http.Server{
Addr: addr,
Handler: ginEngine,
@@ -864,7 +867,7 @@ func startServer(ctx context.Context, config *server.Config) {
" /_/ |_|/_/ |_|\\____//_/ /_/ \\____/ |__/|__/\n",
)
common.Info(fmt.Sprintf("RAGFlow Go Version: %s", common.GetRAGFlowVersion()))
- common.Info(fmt.Sprintf("Server starting on port: %d", config.Server.Port))
+ common.Info(fmt.Sprintf("Server starting on port: %d", config.APIServer.Port))
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
common.Fatal("Failed to start server", zap.Error(err))
}
@@ -873,8 +876,8 @@ func startServer(ctx context.Context, config *server.Config) {
// Start heartbeat reporter to admin server
if hb := startHeartbeat(
common.ServerTypeAPI,
- fmt.Sprintf("ragflow-server-%d", config.Server.Port),
- config.Server.Port,
+ fmt.Sprintf("ragflow-server-%d", config.APIServer.Port),
+ config.APIServer.Port,
config,
); hb != nil {
defer hb.Stop()
diff --git a/internal/admin/handler.go b/internal/admin/handler.go
index a63a4d0876..37ad042f60 100644
--- a/internal/admin/handler.go
+++ b/internal/admin/handler.go
@@ -215,10 +215,10 @@ func (h *Handler) ListUsers(c *gin.Context) {
return
}
+ ctx := c.Request.Context()
var users []map[string]interface{}
switch common.GetRAGFlowType() {
case common.OpenSourceVersion:
- ctx := c.Request.Context()
users, err = h.service.ListUsers(ctx, pageInt, pageSizeInt, name, status, sort, orderBy)
if err != nil {
common.ErrorWithCode(c, common.CodeServerError, err.Error())
@@ -228,7 +228,7 @@ func (h *Handler) ListUsers(c *gin.Context) {
common.SuccessWithData(c, users, "List users")
return
case common.EnterpriseEdition:
- users, err = h.service.ListUsersEE(pageInt, pageSizeInt, name, status, role, sort, orderBy, plan, topInt, daysInt, quotaPtr)
+ users, err = h.service.ListUsersEE(ctx, pageInt, pageSizeInt, name, status, role, sort, orderBy, plan, topInt, daysInt, quotaPtr)
if err != nil {
common.ErrorWithCode(c, common.CodeServerError, err.Error())
return
@@ -865,7 +865,7 @@ func (h *Handler) HandleNoRoute(c *gin.Context) {
// GetLogLevel returns the current log level
func (h *Handler) GetLogLevel(c *gin.Context) {
- level := common.GetLevel()
+ level := common.GetLogLevel()
common.SuccessWithData(c, gin.H{"level": level}, "SUCCESS")
}
@@ -882,7 +882,7 @@ func (h *Handler) SetLogLevel(c *gin.Context) {
return
}
- if err := common.SetLevel(req.Level); err != nil {
+ if err := common.SetLogLevel(req.Level); err != nil {
common.ErrorWithCode(c, common.CodeBadRequest, err.Error())
return
}
@@ -1095,11 +1095,12 @@ func (h *Handler) ListIngestionTasks(c *gin.Context) {
var err error
var tasks []map[string]interface{}
var req ListIngestionTasksRequest
+ ctx := c.Request.Context()
+
if err = c.ShouldBindJSON(&req); err != nil {
- ctx := c.Request.Context()
tasks, err = h.service.ListIngestionTasks(ctx)
} else {
- tasks, err = h.service.ListIngestionTasksByCondition(req.Email, req.Status)
+ tasks, err = h.service.ListIngestionTasksByCondition(ctx, req.Email, req.Status)
}
if err != nil {
diff --git a/internal/admin/handler_ee.go b/internal/admin/handler_ee.go
index 6198e0738d..5ae201fa00 100644
--- a/internal/admin/handler_ee.go
+++ b/internal/admin/handler_ee.go
@@ -887,7 +887,9 @@ func (h *Handler) ShowUserSummary(c *gin.Context) {
return
}
- userSummary, err := h.service.ShowUserSummary(username)
+ ctx := c.Request.Context()
+
+ userSummary, err := h.service.ShowUserSummary(ctx, username)
if err != nil {
if errors.Is(err, common.ErrUserNotFound) {
common.ErrorWithCode(c, common.CodeNotFound, "User not found")
@@ -927,7 +929,9 @@ func (h *Handler) ShowUserQuota(c *gin.Context) {
return
}
- userQuota, err := h.service.ShowUserQuota(username)
+ ctx := c.Request.Context()
+
+ userQuota, err := h.service.ShowUserQuota(ctx, username)
if err != nil {
if errors.Is(err, common.ErrUserNotFound) {
common.ErrorWithCode(c, common.CodeNotFound, "User not found")
@@ -1923,7 +1927,9 @@ func (h *Handler) GetTokenStats(c *gin.Context) {
}
granularity = strings.ToLower(granularity)
- stats, err := h.service.GetTokenStats(userName, fromDate, toDate, granularity)
+ ctx := c.Request.Context()
+
+ stats, err := h.service.GetTokenStats(ctx, userName, fromDate, toDate, granularity)
if err != nil {
common.ErrorWithCode(c, common.CodeDataError, err.Error())
return
@@ -1990,8 +1996,9 @@ func (h *Handler) ListLogs(c *gin.Context) {
common.ErrorWithCode(c, common.CodeBadRequest, "Invalid days")
return
}
+ ctx := c.Request.Context()
- stats, err := h.service.ListLogs(userName, daysInt)
+ stats, err := h.service.ListLogs(ctx, userName, daysInt)
if err != nil {
common.ErrorWithCode(c, common.CodeDataError, err.Error())
return
diff --git a/internal/admin/service_ee.go b/internal/admin/service_ee.go
index d5e2b27f22..2c4bf8f7ed 100644
--- a/internal/admin/service_ee.go
+++ b/internal/admin/service_ee.go
@@ -441,10 +441,10 @@ func (s *Service) ShowUserDatasetSummary(email, dataset string) (map[string]inte
}
// ShowUserSummary show user summary for enterprise edition
-func (s *Service) ShowUserSummary(email string) (map[string]interface{}, error) {
+func (s *Service) ShowUserSummary(ctx context.Context, email string) (map[string]interface{}, error) {
// Query user by email
var user entity.User
- err := dao.DB.Where("email = ?", email).First(&user).Error
+ err := dao.DB.WithContext(ctx).Where("email = ?", email).First(&user).Error
if err != nil {
return nil, common.ErrUserNotFound
}
@@ -477,10 +477,10 @@ func (s *Service) ShowUserStorage(email string) (map[string]interface{}, error)
}
// ShowUserQuota show user quota for enterprise edition
-func (s *Service) ShowUserQuota(email string) (map[string]interface{}, error) {
+func (s *Service) ShowUserQuota(ctx context.Context, email string) (map[string]interface{}, error) {
// Query user by email
var user entity.User
- err := dao.DB.Where("email = ?", email).First(&user).Error
+ err := dao.DB.WithContext(ctx).Where("email = ?", email).First(&user).Error
if err != nil {
return nil, common.ErrUserNotFound
}
@@ -794,7 +794,7 @@ func (s *Service) ShowUsersActivity(days, windows *int) (map[string]interface{},
return result, nil
}
-func (s *Service) ListUsersEE(pageIndex, pageSize int, name string, status, role, sort, orderBy, plan string, top, days int, quota *int) ([]map[string]interface{}, error) {
+func (s *Service) ListUsersEE(ctx context.Context, pageIndex, pageSize int, name string, status, role, sort, orderBy, plan string, top, days int, quota *int) ([]map[string]interface{}, error) {
item := map[string]interface{}{}
item["pageIndex"] = pageIndex
item["pageSize"] = pageSize
@@ -1119,7 +1119,7 @@ func (s *Service) ListUserAPIKeys(ctx context.Context, username string) ([]map[s
return result, nil
}
-func (s *Service) ListIngestionTasksByCondition(email, status *string) ([]map[string]interface{}, error) {
+func (s *Service) ListIngestionTasksByCondition(ctx context.Context, email, status *string) ([]map[string]interface{}, error) {
if email == nil && status == nil {
return nil, fmt.Errorf("email or status are required")
@@ -1299,7 +1299,7 @@ func (s *Service) BatchDeleteWhiteList(ids []int) (map[string]interface{}, error
}
// GetTokenStats returns API token statistics for the user.
-func (s *Service) GetTokenStats(userName, fromDate, toDate, granularity string) ([]map[string]interface{}, error) {
+func (s *Service) GetTokenStats(ctx context.Context, userName, fromDate, toDate, granularity string) ([]map[string]interface{}, error) {
result := []map[string]interface{}{
{
"command": "get_token_stats",
@@ -1340,7 +1340,7 @@ func (s *Service) GetTokenStatsSummary(fromDate, toDate string) (map[string]inte
}
// ListLogs lists operation logs for the user.
-func (s *Service) ListLogs(userName string, days int) ([]map[string]interface{}, error) {
+func (s *Service) ListLogs(ctx context.Context, userName string, days int) ([]map[string]interface{}, error) {
result := []map[string]interface{}{
{
"command": "list_logs",
diff --git a/internal/common/logger.go b/internal/common/logger.go
index cc98fcfc70..61bfbf01b5 100644
--- a/internal/common/logger.go
+++ b/internal/common/logger.go
@@ -17,14 +17,14 @@
package common
import (
- "errors"
"fmt"
"os"
"path/filepath"
- "runtime"
"strings"
"time"
+ "github.com/pkg/errors"
+
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
@@ -108,7 +108,7 @@ func Init(level string, file FileOutput, serviceName string) error {
TimeKey: "timestamp",
LevelKey: "level",
NameKey: "service",
- CallerKey: "",
+ CallerKey: "caller",
FunctionKey: "",
MessageKey: "msg",
StacktraceKey: "stacktrace",
@@ -167,26 +167,20 @@ func Init(level string, file FileOutput, serviceName string) error {
return nil
}
-// Sync flushes any buffered log entries.
-func Sync() {
+// SyncLog flushes any buffered log entries.
+func SyncLog() {
if Logger != nil {
_ = Logger.Sync()
}
}
-// Fatal logs a fatal message using zap with caller info, then calls os.Exit(1).
func Fatal(msg string, fields ...zap.Field) {
if Logger == nil {
panic("logger not initialized")
}
- _, file, line, ok := runtime.Caller(1)
- if ok {
- fields = append(fields, zap.String("caller", fmt.Sprintf("%s:%d", file, line)))
- }
Logger.Fatal(msg, fields...)
}
-// Info logs an info message.
func Info(msg string, fields ...zap.Field) {
if Logger == nil {
return
@@ -194,19 +188,18 @@ func Info(msg string, fields ...zap.Field) {
Logger.Info(msg, fields...)
}
-// Error logs an error message. err may be nil; if non-nil it is appended as
-// a zap.Error field. Additional fields follow.
func Error(msg string, err error, fields ...zap.Field) {
if Logger == nil {
return
}
- if err != nil {
- fields = append(fields, zap.Error(err))
+
+ if IsDebugEnabled() {
+ Logger.Error(fmt.Sprintf("%s, %+v", msg, err), fields...)
+ } else {
+ Logger.Error(fmt.Sprintf("%s, %v", msg, err), fields...)
}
- Logger.Error(msg, fields...)
}
-// Debug logs a debug message.
func Debug(msg string, fields ...zap.Field) {
if Logger == nil {
return
@@ -214,7 +207,6 @@ func Debug(msg string, fields ...zap.Field) {
Logger.Debug(msg, fields...)
}
-// Warn logs a warning message.
func Warn(msg string, fields ...zap.Field) {
if Logger == nil {
return
@@ -227,13 +219,13 @@ func IsDebugEnabled() bool {
return atomicLevel.Enabled(zapcore.DebugLevel)
}
-// GetLevel returns the current log level.
-func GetLevel() string {
+// GetLogLevel returns the current log level.
+func GetLogLevel() string {
return atomicLevel.String()
}
-// SetLevel sets the log level at runtime.
-func SetLevel(level string) error {
+// SetLogLevel sets the log level at runtime.
+func SetLogLevel(level string) error {
zapLevel, err := parseZapLevel(level)
if err != nil {
return err
@@ -242,15 +234,6 @@ func SetLevel(level string) error {
return nil
}
-// ResolveCompress applies the project default (true) when the config-level
-// Compress is nil. When non-nil, the operator's choice is used as-is.
-//
-// The project default is compression on; operators can opt out by setting
-// log.compress: false in service_conf.yaml. Because Go's bool zero value is
-// false and would otherwise be indistinguishable from "not set", the YAML
-// struct uses *bool and this helper resolves the defaulting at the cmd/
-// boundary. The *bool does not live in this file because FileOutput itself
-// takes a plain bool (the caller has already resolved the default by then).
func ResolveCompress(c *bool) bool {
if c == nil {
return true
@@ -319,7 +302,7 @@ func GinLogger() gin.HandlerFunc {
// Likely a panic recovered by gin.Recovery() with no c.Error attached.
// Use a sentinel so the err field is non-empty; operators can
// grep for this string in logs.
- ginErr = errors.New("5xx response with no handler error attached")
+ ginErr = err5xxNoError
}
Error(msg, ginErr, fields...)
case status >= 400:
@@ -329,3 +312,5 @@ func GinLogger() gin.HandlerFunc {
}
}
}
+
+var err5xxNoError = errors.New("5xx response with no handler error attached")
diff --git a/internal/dao/database.go b/internal/dao/database.go
index 97b235fd03..a40e4bf059 100644
--- a/internal/dao/database.go
+++ b/internal/dao/database.go
@@ -81,7 +81,7 @@ func InitDB(ctx context.Context, migrateDB bool) error {
// Set log level
var gormLogLevel gormLogger.LogLevel
- if cfg.Server.Mode == "debug" {
+ if cfg.General.Mode == "debug" {
gormLogLevel = gormLogger.Info
} else {
gormLogLevel = gormLogger.Silent
diff --git a/internal/dao/user.go b/internal/dao/user.go
index 94d38ff71a..7e9b647503 100644
--- a/internal/dao/user.go
+++ b/internal/dao/user.go
@@ -18,7 +18,6 @@ package dao
import (
"context"
-
"ragflow/internal/entity"
"gorm.io/gorm"
diff --git a/internal/handler/system.go b/internal/handler/system.go
index f7b767f979..0b2db57a4d 100644
--- a/internal/handler/system.go
+++ b/internal/handler/system.go
@@ -144,7 +144,7 @@ func (h *SystemHandler) GetVersion(c *gin.Context) {
// table carried (e.g. "peewee", "pdfminer") were inert for the Go
// side and are no longer returned.
func (h *SystemHandler) GetLogLevel(c *gin.Context) {
- common.SuccessWithData(c, gin.H{"level": common.GetLevel()}, "success")
+ common.SuccessWithData(c, gin.H{"level": common.GetLogLevel()}, "success")
}
// SetLogLevelRequest set log level request. PkgName is accepted for
@@ -172,13 +172,13 @@ func (h *SystemHandler) SetLogLevel(c *gin.Context) {
return
}
- if err := common.SetLevel(req.Level); err != nil {
+ if err := common.SetLogLevel(req.Level); err != nil {
common.ErrorWithCode(c, common.CodeDataError, "Invalid log level: "+req.Level)
return
}
if config := server.GetConfig(); config != nil {
- config.Log.Level = common.GetLevel()
+ config.Log.Level = common.GetLogLevel()
}
common.SuccessWithData(c, gin.H{"level": req.Level}, "SUCCESS")
diff --git a/internal/server/config.go b/internal/server/config.go
index cfa988380d..3a1e643fd0 100644
--- a/internal/server/config.go
+++ b/internal/server/config.go
@@ -17,6 +17,7 @@
package server
import (
+ "errors"
"fmt"
"net"
"net/mail"
@@ -37,7 +38,6 @@ const DefaultConnectTimeout = 5 * time.Second
// Config application configuration
type Config struct {
General GeneralConfig `mapstructure:"general"`
- Server ServerConfig `mapstructure:"server"`
Authentication AuthenticationConfig `mapstructure:"authentication"`
Database DatabaseConfig `mapstructure:"database"`
Redis RedisConfig `mapstructure:"redis"`
@@ -49,6 +49,7 @@ type Config struct {
OAuth map[string]OAuthConfig `mapstructure:"oauth"`
SMTP common.SMTPConfig `mapstructure:"smtp"`
Admin AdminConfig `mapstructure:"admin"`
+ APIServer APIServerConfig `mapstructure:"ragflow"`
UserDefaultLLM UserDefaultLLMConfig `mapstructure:"user_default_llm"`
DefaultSuperUser DefaultSuperUser `mapstructure:"default_super_user"`
Language string `mapstructure:"language"`
@@ -61,6 +62,13 @@ type Config struct {
// GeneralConfig general configuration
type GeneralConfig struct {
HeartbeatInterval time.Duration `mapstructure:"heartbeat_interval"`
+ Mode string `mapstructure:"mode"` // debug, release
+ SecretKey *string `mapstructure:"secret_key"`
+ DocEngine string `mapstructure:"doc_engine"` // Infinity, Elasticsearch
+ StorageEngine string `mapstructure:"storage_engine"` // Minio, S3
+ CacheEngine string `mapstructure:"cache_engine"` // Redis
+ QueueEngine string `mapstructure:"queue_engine"` // NATS
+ AnalyticEngine string `mapstructure:"analytic_engine"` // Clickhouse
}
// AdminConfig admin server configuration
@@ -69,6 +77,11 @@ type AdminConfig struct {
Port int `mapstructure:"http_port"`
}
+type APIServerConfig struct {
+ Host string `mapstructure:"host"`
+ Port int `mapstructure:"http_port"`
+}
+
type AuthenticationConfig struct {
DisablePasswordLogin bool `mapstructure:"disable_password_login"`
RegisterEnabled bool `mapstructure:"register_enabled"`
@@ -135,7 +148,7 @@ type ModelConfig struct {
// OAuthConfig OAuth configuration for a channel.
// Mirrors api/apps/auth/__init__.py's OAUTH_CONFIG entries: a Type that
-// selects the auth client flavor (oauth2 / oidc / github), plus the
+// selects the auth client flavor (oauth2 / oidc / GitHub), plus the
// transport URLs and client credentials. For OIDC the URLs are derived
// from Issuer via the .well-known/openid-configuration document, so they
// may be left blank.
@@ -153,13 +166,6 @@ type OAuthConfig struct {
Issuer string `mapstructure:"issuer"`
}
-// ServerConfig server configuration
-type ServerConfig struct {
- Mode string `mapstructure:"mode"` // debug, release
- Port int `mapstructure:"port"`
- SecretKey *string `mapstructure:"secret_key"`
-}
-
// DatabaseConfig database configuration
type DatabaseConfig struct {
Driver string `mapstructure:"driver"` // mysql
@@ -485,7 +491,7 @@ func Init(configPath string) error {
func FromEnvironments() error {
// Secret key
if envVal := common.GetEnv(common.EnvRAGFlowSecretKey); envVal != "" {
- globalConfig.Server.SecretKey = &envVal
+ globalConfig.General.SecretKey = &envVal
}
// Load REGISTER_ENABLED from environment variable (default: true)
@@ -593,7 +599,7 @@ func FromEnvironments() error {
minioRegion := strings.ToLower(common.GetEnv(common.EnvMinioRegion))
if minioRegion != "" {
if globalConfig.StorageEngine.Minio == nil {
- return fmt.Errorf("Minio config not found")
+ return fmt.Errorf("minio config not found")
}
globalConfig.StorageEngine.Minio.Region = minioRegion
}
@@ -641,7 +647,8 @@ func FromConfigFile(configPath string) error {
// Read configuration file
if err := v.ReadInConfig(); err != nil {
- if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
+ var configFileNotFoundError viper.ConfigFileNotFoundError
+ if !errors.As(err, &configFileNotFoundError) {
return fmt.Errorf("read config file error: %w", err)
}
zapLogger.Info("Config file not found, using environment variables only")
@@ -702,25 +709,31 @@ func FromConfigFile(configPath string) error {
}
// Map ragflow section to ServerConfig
- if globalConfig != nil && globalConfig.Server.Port == 0 {
+ if globalConfig != nil && globalConfig.APIServer.Port == 0 {
// Try to map from ragflow section
if v.IsSet("ragflow") {
ragflowConfig := v.Sub("ragflow")
if ragflowConfig != nil {
- globalConfig.Server.Port = ragflowConfig.GetInt("http_port") + 4 // 9384, by default
+ globalConfig.APIServer.Port = ragflowConfig.GetInt("http_port") + 4 // 9384, by default
//globalConfig.Server.Port = ragflowConfig.GetInt("http_port") // Correct
// If mode is not set, default to debug
- if globalConfig.Server.Mode == "" {
- globalConfig.Server.Mode = "release"
+ if globalConfig.General.Mode == "" {
+ globalConfig.General.Mode = "release"
}
secretKey := ragflowConfig.GetString("secret_key")
if secretKey != "" {
- globalConfig.Server.SecretKey = &secretKey
+ globalConfig.General.SecretKey = &secretKey
}
}
}
}
+ if globalConfig.APIServer.Port == 0 {
+ globalConfig.APIServer.Port = 9384
+ } else {
+ globalConfig.APIServer.Port += 4
+ }
+
// Map redis section to RedisConfig
if globalConfig != nil && globalConfig.Redis.Host != "" {
if v.IsSet("redis") {
@@ -729,7 +742,7 @@ func FromConfigFile(configPath string) error {
hostStr := redisConfig.GetString("host")
// Handle host:port format (e.g., "localhost:6379")
if hostStr == "" {
- return fmt.Errorf("Empty host of redis configuration")
+ return fmt.Errorf("empty host of Redis configuration")
}
if idx := strings.LastIndex(hostStr, ":"); idx != -1 {
@@ -740,7 +753,7 @@ func FromConfigFile(configPath string) error {
}
}
} else {
- return fmt.Errorf("Error address format of redis: %s", hostStr)
+ return fmt.Errorf("error address format of Redis: %s", hostStr)
}
globalConfig.Redis.Password = redisConfig.GetString("password")
@@ -925,7 +938,7 @@ func FromConfigFile(configPath string) error {
return nil
}
-// Get get global configuration
+// GetConfig gets the global configuration
func GetConfig() *Config {
return globalConfig
}
@@ -943,10 +956,6 @@ func SetLogger(l *zap.Logger) {
zapLogger = l
}
-func GetGlobalViperConfig() *viper.Viper {
- return globalViper
-}
-
func GetAllConfigs() []map[string]interface{} {
return allConfigs
}
diff --git a/internal/server/variable.go b/internal/server/variable.go
index 94e229fdd6..5d0a42f169 100644
--- a/internal/server/variable.go
+++ b/internal/server/variable.go
@@ -91,8 +91,8 @@ func InitVariables(store VariableStore) error {
// GetSecretKey returns the current secret key
func GetSecretKey(store VariableStore) (string, error) {
- if globalConfig.Server.SecretKey != nil {
- return *globalConfig.Server.SecretKey, nil
+ if globalConfig.General.SecretKey != nil {
+ return *globalConfig.General.SecretKey, nil
}
generatedKey, err := utility.GenerateSecretKey()
diff --git a/internal/service/user.go b/internal/service/user.go
index ebe1d27df4..90ca4591b8 100644
--- a/internal/service/user.go
+++ b/internal/service/user.go
@@ -23,7 +23,6 @@ import (
"crypto/sha512"
"encoding/base64"
"encoding/hex"
- "errors"
"fmt"
"hash"
"ragflow/internal/common"
@@ -35,6 +34,8 @@ import (
"strings"
"time"
+ "github.com/pkg/errors"
+
"golang.org/x/crypto/pbkdf2"
"golang.org/x/crypto/scrypt"
"gorm.io/gorm"
@@ -392,6 +393,7 @@ func (s *UserService) Login(ctx context.Context, req *LoginRequest) (*entity.Use
func (s *UserService) LoginByEmail(ctx context.Context, req *EmailLoginRequest) (*entity.User, common.ErrorCode, error) {
user, err := s.userDAO.GetByEmail(ctx, dao.DB, req.Email)
if err != nil {
+ common.Error("user not found by email", err)
return nil, common.CodeAuthenticationError, fmt.Errorf("email: %s is not registered", req.Email)
}