Files
crawlab/core/middlewares/cors.go
2024-11-18 16:48:09 +08:00

24 lines
632 B
Go

package middlewares
import (
"github.com/crawlab-team/crawlab/core/utils"
"github.com/gin-gonic/gin"
"net/http"
)
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", utils.GetAllowOrigin())
c.Writer.Header().Set("Access-Control-Allow-Credentials", utils.GetAllowCredentials())
c.Writer.Header().Set("Access-Control-Allow-Headers", utils.GetAllowHeaders())
c.Writer.Header().Set("Access-Control-Allow-Methods", utils.GetAllowMethods())
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(http.StatusNoContent)
return
}
c.Next()
}
}