12345678910111213141516171819202122232425262728293031323334353637383940 |
- package middleware
- import (
- "net/http"
- "github.com/gin-contrib/cors"
- "github.com/gin-gonic/gin"
- )
- func CORS(configs ...cors.Config) gin.HandlerFunc {
- if len(configs) != 0 {
- return cors.New(configs[0])
- }
- return func(c *gin.Context) {
- method := c.Request.Method
- origin := c.Request.Header.Get("Origin")
- if origin != "" {
-
- c.Writer.Header().Set("Access-Control-Allow-Origin", origin)
-
- c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE,UPDATE")
-
- c.Header("Access-Control-Allow-Headers", "Authorization, Content-Length")
-
- c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers")
-
- c.Header("Access-Control-Max-Age", "172800")
-
- c.Header("Access-Control-Allow-Credentials", "true")
- }
-
- if method == "OPTIONS" {
- c.JSON(http.StatusOK, "ok!")
- return
- }
- c.Next()
- }
- }
|