feat: 路由优化

This commit is contained in:
tbphp
2025-07-01 00:13:01 +08:00
parent b9a833ceab
commit d5ebbf10f5
12 changed files with 206 additions and 142 deletions

View File

@@ -48,15 +48,16 @@ func InitDB() (*gorm.DB, error) {
sqlDB.SetMaxOpenConns(100)
sqlDB.SetConnMaxLifetime(time.Hour)
// Auto-migrate models
err = DB.AutoMigrate(
&models.SystemSetting{},
&models.Group{},
&models.APIKey{},
&models.RequestLog{},
)
if err != nil {
return nil, fmt.Errorf("failed to auto-migrate database: %w", err)
if os.Getenv("DB_AUTO_MIGRATE") != "false" {
err = DB.AutoMigrate(
&models.SystemSetting{},
&models.Group{},
&models.APIKey{},
&models.RequestLog{},
)
if err != nil {
return nil, fmt.Errorf("failed to auto-migrate database: %w", err)
}
}
fmt.Println("Database connection initialized and models migrated.")

View File

@@ -42,13 +42,11 @@ func (s *Server) RegisterAPIRoutes(api *gin.RouterGroup) {
{
keys.POST("", s.CreateKeysInGroup)
keys.GET("", s.ListKeysInGroup)
keys.PUT("/:key_id", s.UpdateKey)
keys.DELETE("", s.DeleteKeys)
}
}
// Key management routes
api.PUT("/keys/:key_id", s.UpdateKey)
api.DELETE("/keys", s.DeleteKeys)
// Dashboard and logs routes
dashboard := api.Group("/dashboard")
{
@@ -100,15 +98,6 @@ func (s *Server) Health(c *gin.Context) {
})
}
// MethodNotAllowed handles 405 requests
func (s *Server) MethodNotAllowed(c *gin.Context) {
c.JSON(http.StatusMethodNotAllowed, gin.H{
"error": "Method not allowed",
"path": c.Request.URL.Path,
"method": c.Request.Method,
"timestamp": time.Now().UTC().Format(time.RFC3339),
})
}
// GetConfig returns configuration information (for debugging)
func (s *Server) GetConfig(c *gin.Context) {

View File

@@ -64,6 +64,12 @@ func (s *Server) ListKeysInGroup(c *gin.Context) {
// UpdateKey handles updating a specific key.
func (s *Server) UpdateKey(c *gin.Context) {
groupID, err := strconv.Atoi(c.Param("id"))
if err != nil {
response.Error(c, http.StatusBadRequest, "Invalid group ID")
return
}
keyID, err := strconv.Atoi(c.Param("key_id"))
if err != nil {
response.Error(c, http.StatusBadRequest, "Invalid key ID")
@@ -71,8 +77,8 @@ func (s *Server) UpdateKey(c *gin.Context) {
}
var key models.APIKey
if err := s.DB.First(&key, keyID).Error; err != nil {
response.Error(c, http.StatusNotFound, "Key not found")
if err := s.DB.Where("group_id = ? AND id = ?", groupID, keyID).First(&key).Error; err != nil {
response.Error(c, http.StatusNotFound, "Key not found in this group")
return
}
@@ -99,6 +105,12 @@ type DeleteKeysRequest struct {
// DeleteKeys handles deleting one or more keys.
func (s *Server) DeleteKeys(c *gin.Context) {
groupID, err := strconv.Atoi(c.Param("id"))
if err != nil {
response.Error(c, http.StatusBadRequest, "Invalid group ID")
return
}
var req DeleteKeysRequest
if err := c.ShouldBindJSON(&req); err != nil {
response.Error(c, http.StatusBadRequest, "Invalid request body")
@@ -110,10 +122,30 @@ func (s *Server) DeleteKeys(c *gin.Context) {
return
}
if err := s.DB.Delete(&models.APIKey{}, req.KeyIDs).Error; err != nil {
// Start a transaction
tx := s.DB.Begin()
// Verify all keys belong to the specified group
var count int64
if err := tx.Model(&models.APIKey{}).Where("id IN ? AND group_id = ?", req.KeyIDs, groupID).Count(&count).Error; err != nil {
tx.Rollback()
response.Error(c, http.StatusInternalServerError, "Failed to verify keys")
return
}
if count != int64(len(req.KeyIDs)) {
tx.Rollback()
response.Error(c, http.StatusForbidden, "One or more keys do not belong to the specified group")
return
}
// Delete the keys
if err := tx.Where("id IN ?", req.KeyIDs).Delete(&models.APIKey{}).Error; err != nil {
tx.Rollback()
response.Error(c, http.StatusInternalServerError, "Failed to delete keys")
return
}
tx.Commit()
response.Success(c, gin.H{"message": "Keys deleted successfully"})
}

88
internal/router/router.go Normal file
View File

@@ -0,0 +1,88 @@
package router
import (
"gpt-load/internal/handler"
"gpt-load/internal/middleware"
"gpt-load/internal/proxy"
"gpt-load/internal/types"
"io/fs"
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
)
// New 创建并配置一个完整的 gin.Engine 实例
func New(
serverHandler *handler.Server,
proxyServer *proxy.ProxyServer,
configManager types.ConfigManager,
webUI fs.FS,
) *gin.Engine {
// 设置 Gin 模式
gin.SetMode(gin.ReleaseMode)
router := gin.New()
// 注册全局中间件
router.Use(middleware.Recovery())
router.Use(middleware.ErrorHandler())
router.Use(middleware.Logger(configManager.GetLogConfig()))
router.Use(middleware.CORS(configManager.GetCORSConfig()))
router.Use(middleware.RateLimiter(configManager.GetPerformanceConfig()))
// 添加服务器启动时间中间件
startTime := time.Now()
router.Use(func(c *gin.Context) {
c.Set("serverStartTime", startTime)
c.Next()
})
// 注册 Web UI 和通用端点
router.GET("/health", serverHandler.Health)
router.GET("/stats", serverHandler.Stats)
router.GET("/config", serverHandler.GetConfig) // Debug endpoint
// 注册管理 API 路由
api := router.Group("/api")
authConfig := configManager.GetAuthConfig()
if authConfig.Enabled {
api.Use(middleware.Auth(authConfig))
}
serverHandler.RegisterAPIRoutes(api)
// 注册代理路由
proxyGroup := router.Group("/proxy")
if authConfig.Enabled {
proxyGroup.Use(middleware.Auth(authConfig))
}
proxyServer.RegisterProxyRoutes(proxyGroup)
// 处理 405 Method Not Allowed
router.NoMethod(func(c *gin.Context) {
c.JSON(http.StatusMethodNotAllowed, gin.H{"error": "Method not allowed"})
})
// 其他所有路由都交给前端 UI 处理
router.NoRoute(ServeUI(webUI))
return router
}
// ServeUI 返回一个 gin.HandlerFunc 来服务嵌入式前端 UI
func ServeUI(webUI fs.FS) gin.HandlerFunc {
fileServer := http.FileServer(http.FS(webUI))
return func(c *gin.Context) {
// 检查文件是否存在于嵌入的文件系统中
if _, err := webUI.Open(strings.TrimPrefix(c.Request.URL.Path, "/")); err != nil {
// 如果文件不存在并且不是API或代理请求则将请求重写为 /
// 这将提供 index.html以支持 SPA 的前端路由
if !strings.HasPrefix(c.Request.URL.Path, "/api/") && !strings.HasPrefix(c.Request.URL.Path, "/proxy/") {
c.Request.URL.Path = "/"
}
}
fileServer.ServeHTTP(c.Writer, c.Request)
}
}