github.com/lovung/GoCleanArchitecture@v0.0.0-20210302152432-50d91fd29f9f/app/internal/interface/restful/middleware/cors_middleware.go (about) 1 package middleware 2 3 import ( 4 "sync" 5 "time" 6 7 "github.com/gin-contrib/cors" 8 "github.com/gin-gonic/gin" 9 ) 10 11 const ( 12 localHost = "http://localhost:3000" 13 ) 14 15 var ( 16 corsOnce sync.Once 17 corsMiddleware gin.HandlerFunc 18 ) 19 20 // CorsMiddleware return the middeware instance 21 func CorsMiddleware() gin.HandlerFunc { 22 corsOnce.Do(func() { 23 corsMiddleware = cors.New(cors.Config{ 24 AllowOrigins: []string{localHost}, 25 AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"}, 26 AllowHeaders: []string{ 27 "Content-Length", 28 "Origin", 29 "cookie", 30 "authorization", 31 "origin", 32 "content-type", 33 "Content-Type", 34 "accept", 35 "X-CSRF-Token", 36 "x-requested-with", 37 "Cache-Control", 38 }, 39 ExposeHeaders: []string{"Content-Length", "Content-Disposition"}, 40 AllowCredentials: true, 41 MaxAge: 12 * time.Hour, 42 }) 43 }) 44 return corsMiddleware 45 }