feat: 配置分离
This commit is contained in:
@@ -49,7 +49,7 @@ func (s *Server) Login(c *gin.Context) {
|
||||
}
|
||||
|
||||
authConfig := s.config.GetAuthConfig()
|
||||
|
||||
|
||||
if !authConfig.Enabled {
|
||||
c.JSON(http.StatusOK, LoginResponse{
|
||||
Success: true,
|
||||
@@ -102,70 +102,3 @@ func (s *Server) Health(c *gin.Context) {
|
||||
"uptime": uptime,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
// GetConfig returns configuration information (for debugging)
|
||||
func (s *Server) GetConfig(c *gin.Context) {
|
||||
// Only allow in development mode or with special header
|
||||
if c.GetHeader("X-Debug-Config") != "true" {
|
||||
c.JSON(http.StatusForbidden, gin.H{
|
||||
"error": "Access denied",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
serverConfig := s.config.GetServerConfig()
|
||||
openaiConfig := s.config.GetOpenAIConfig()
|
||||
authConfig := s.config.GetAuthConfig()
|
||||
corsConfig := s.config.GetCORSConfig()
|
||||
perfConfig := s.config.GetPerformanceConfig()
|
||||
logConfig := s.config.GetLogConfig()
|
||||
|
||||
// Sanitize sensitive information
|
||||
sanitizedConfig := gin.H{
|
||||
"server": gin.H{
|
||||
"host": serverConfig.Host,
|
||||
"port": serverConfig.Port,
|
||||
},
|
||||
"openai": gin.H{
|
||||
"base_url": openaiConfig.BaseURL,
|
||||
"request_timeout": openaiConfig.RequestTimeout,
|
||||
"response_timeout": openaiConfig.ResponseTimeout,
|
||||
"idle_conn_timeout": openaiConfig.IdleConnTimeout,
|
||||
},
|
||||
"auth": gin.H{
|
||||
"enabled": authConfig.Enabled,
|
||||
// Don't expose the actual key
|
||||
},
|
||||
"cors": gin.H{
|
||||
"enabled": corsConfig.Enabled,
|
||||
"allowed_origins": corsConfig.AllowedOrigins,
|
||||
"allowed_methods": corsConfig.AllowedMethods,
|
||||
"allowed_headers": corsConfig.AllowedHeaders,
|
||||
"allow_credentials": corsConfig.AllowCredentials,
|
||||
},
|
||||
"performance": gin.H{
|
||||
"max_concurrent_requests": perfConfig.MaxConcurrentRequests,
|
||||
"enable_gzip": perfConfig.EnableGzip,
|
||||
},
|
||||
"timeout_config": gin.H{
|
||||
"request_timeout_s": openaiConfig.RequestTimeout,
|
||||
"response_timeout_s": openaiConfig.ResponseTimeout,
|
||||
"idle_conn_timeout_s": openaiConfig.IdleConnTimeout,
|
||||
"server_read_timeout_s": serverConfig.ReadTimeout,
|
||||
"server_write_timeout_s": serverConfig.WriteTimeout,
|
||||
"server_idle_timeout_s": serverConfig.IdleTimeout,
|
||||
"graceful_shutdown_timeout_s": serverConfig.GracefulShutdownTimeout,
|
||||
},
|
||||
"log": gin.H{
|
||||
"level": logConfig.Level,
|
||||
"format": logConfig.Format,
|
||||
"enable_file": logConfig.EnableFile,
|
||||
"file_path": logConfig.FilePath,
|
||||
"enable_request": logConfig.EnableRequest,
|
||||
},
|
||||
"timestamp": time.Now().UTC().Format(time.RFC3339),
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, sanitizedConfig)
|
||||
}
|
||||
|
34
internal/handler/log_cleanup_handler.go
Normal file
34
internal/handler/log_cleanup_handler.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"gpt-load/internal/response"
|
||||
"gpt-load/internal/services"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// LogCleanupHandler handles log cleanup related requests
|
||||
type LogCleanupHandler struct {
|
||||
LogCleanupService *services.LogCleanupService
|
||||
}
|
||||
|
||||
// NewLogCleanupHandler creates a new LogCleanupHandler
|
||||
func NewLogCleanupHandler(s *services.LogCleanupService) *LogCleanupHandler {
|
||||
return &LogCleanupHandler{
|
||||
LogCleanupService: s,
|
||||
}
|
||||
}
|
||||
|
||||
// CleanupLogsNow handles the POST /api/logs/cleanup request.
|
||||
// It triggers an asynchronous cleanup of expired request logs.
|
||||
func (h *LogCleanupHandler) CleanupLogsNow(c *gin.Context) {
|
||||
go func() {
|
||||
logrus.Info("Asynchronous log cleanup started from API request")
|
||||
h.LogCleanupService.CleanupNow()
|
||||
}()
|
||||
|
||||
response.Success(c, gin.H{
|
||||
"message": "Log cleanup process started in the background",
|
||||
})
|
||||
}
|
@@ -1,26 +0,0 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"gpt-load/internal/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// ReloadConfig handles the POST /api/reload request.
|
||||
// It triggers a configuration reload.
|
||||
func (s *Server) ReloadConfig(c *gin.Context) {
|
||||
if s.config == nil {
|
||||
response.InternalError(c, "Configuration manager is not initialized")
|
||||
return
|
||||
}
|
||||
|
||||
err := s.config.ReloadConfig()
|
||||
if err != nil {
|
||||
logrus.Errorf("Failed to reload config: %v", err)
|
||||
response.InternalError(c, "Failed to reload config")
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, gin.H{"message": "Configuration reloaded successfully"})
|
||||
}
|
@@ -1,33 +1,28 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"gpt-load/internal/db"
|
||||
"gpt-load/internal/models"
|
||||
"gpt-load/internal/config"
|
||||
"gpt-load/internal/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm/clause"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// GetSettings handles the GET /api/settings request.
|
||||
// It retrieves all system settings from the database and returns them as a key-value map.
|
||||
// It retrieves all system settings and returns them with detailed information.
|
||||
func GetSettings(c *gin.Context) {
|
||||
var settings []models.SystemSetting
|
||||
if err := db.DB.Find(&settings).Error; err != nil {
|
||||
response.InternalError(c, "Failed to retrieve settings")
|
||||
return
|
||||
}
|
||||
settingsManager := config.GetSystemSettingsManager()
|
||||
currentSettings := settingsManager.GetSettings()
|
||||
|
||||
settingsMap := make(map[string]string)
|
||||
for _, s := range settings {
|
||||
settingsMap[s.SettingKey] = s.SettingValue
|
||||
}
|
||||
// 使用新的动态元数据生成器
|
||||
settingsInfo := config.GenerateSettingsMetadata(¤tSettings)
|
||||
|
||||
response.Success(c, settingsMap)
|
||||
response.Success(c, settingsInfo)
|
||||
}
|
||||
|
||||
// UpdateSettings handles the PUT /api/settings request.
|
||||
// It receives a key-value JSON object and updates or creates settings in the database.
|
||||
// It receives a key-value JSON object and updates system settings.
|
||||
// After updating, it triggers a configuration reload.
|
||||
func UpdateSettings(c *gin.Context) {
|
||||
var settingsMap map[string]string
|
||||
if err := c.ShouldBindJSON(&settingsMap); err != nil {
|
||||
@@ -35,28 +30,37 @@ func UpdateSettings(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var settingsToUpdate []models.SystemSetting
|
||||
for key, value := range settingsMap {
|
||||
settingsToUpdate = append(settingsToUpdate, models.SystemSetting{
|
||||
SettingKey: key,
|
||||
SettingValue: value,
|
||||
})
|
||||
}
|
||||
|
||||
if len(settingsToUpdate) == 0 {
|
||||
if len(settingsMap) == 0 {
|
||||
response.Success(c, nil)
|
||||
return
|
||||
}
|
||||
|
||||
// Using OnConflict to perform an "upsert" operation.
|
||||
// If a setting with the same key exists, it will be updated. Otherwise, a new one will be created.
|
||||
if err := db.DB.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "setting_key"}},
|
||||
DoUpdates: clause.AssignmentColumns([]string{"setting_value"}),
|
||||
}).Create(&settingsToUpdate).Error; err != nil {
|
||||
response.InternalError(c, "Failed to update settings")
|
||||
settingsManager := config.GetSystemSettingsManager()
|
||||
|
||||
// 更新配置
|
||||
if err := settingsManager.UpdateSettings(settingsMap); err != nil {
|
||||
response.InternalError(c, "Failed to update settings: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(c, nil)
|
||||
}
|
||||
// 重载系统配置
|
||||
if err := settingsManager.LoadFromDatabase(); err != nil {
|
||||
logrus.Errorf("Failed to reload system settings: %v", err)
|
||||
response.InternalError(c, "Failed to reload system settings")
|
||||
return
|
||||
}
|
||||
|
||||
settingsManager.DisplayCurrentSettings()
|
||||
|
||||
logrus.Info("Configuration reloaded successfully via API")
|
||||
response.Success(c, gin.H{
|
||||
"message": "Configuration reloaded successfully",
|
||||
"timestamp": gin.H{
|
||||
"reloaded_at": "now",
|
||||
},
|
||||
})
|
||||
|
||||
response.Success(c, gin.H{
|
||||
"message": "Settings updated successfully. Configuration reloaded.",
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user