第一次提交

This commit is contained in:
2026-02-02 15:16:11 +08:00
parent 60c06d545d
commit 6036b00a9b
15 changed files with 549 additions and 14 deletions
+37
View File
@@ -0,0 +1,37 @@
package middleware
import (
"net/http"
"git.magicany.cc/black1552/gin-base/log"
"git.magicany.cc/black1552/gin-base/response"
"github.com/gin-gonic/gin"
)
func ErrorHandler() gin.HandlerFunc {
return func(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
log.Error("发生panic", "path", c.Request.URL.Path, "method", c.Request.Method, err)
response.Error(c).SetCode(http.StatusInternalServerError).SetMsg("服务器内部错误").End()
c.Abort()
}
}()
c.Next()
}
}
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
}
}