1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package middleware
import ( "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" )
func Cors() gin.HandlerFunc { config := cors.DefaultConfig() config.AllowMethods = []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"} config.AllowHeaders = []string{"Origin", "Content-Length", "Content-Type", "Cookie"} if gin.Mode() == gin.ReleaseMode { config.AllowOrigins = []string{"http://www.example.com"} } else { config.AllowOrigins = []string{"*"} } config.AllowCredentials = true return cors.New(config) }
|