package main import ( "net/http" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" ) // corsMiddleware CORS 中间件 func corsMiddleware() gin.HandlerFunc { return func(c *gin.Context) { c.Header("Access-Control-Allow-Origin", "*") c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") c.Header("Access-Control-Allow-Headers", "Content-Type, Authorization") if c.Request.Method == "OPTIONS" { c.AbortWithStatus(http.StatusNoContent) return } c.Next() } } // errorHandlingMiddleware 错误处理中间件 func errorHandlingMiddleware() gin.HandlerFunc { return gin.CustomRecovery(func(c *gin.Context, recovered any) { logrus.Errorf("服务器内部错误: %v, path: %s", recovered, c.Request.URL.Path) respondError(c, http.StatusInternalServerError, "INTERNAL_ERROR", "服务器内部错误", recovered) }) }