cors.go 842 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package middleware
  2. import (
  3. "github.com/gin-contrib/cors"
  4. "github.com/gin-gonic/gin"
  5. "time"
  6. )
  7. var defaultCORSConfig = cors.Config{
  8. AllowAllOrigins: true,
  9. AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD"},
  10. AllowHeaders: []string{
  11. "Origin",
  12. "Accept",
  13. "Accept-Language",
  14. "Content-Language",
  15. "Content-Type",
  16. "User-Agent",
  17. "Authorization",
  18. "x-timezone-offset",
  19. "x-user-id",
  20. "X-User-Id",
  21. "X-Rate-Limit-Token",
  22. "x-rate-limit-token",
  23. "X-Timezone-Name",
  24. "x-timezone-name",
  25. "X-Lingochamp-Id",
  26. "x-lingochamp-id",
  27. "x-user-language",
  28. },
  29. AllowCredentials: true,
  30. MaxAge: 12 * time.Hour,
  31. }
  32. // CORS enable CORS support
  33. func CORS(configs ...cors.Config) gin.HandlerFunc {
  34. if len(configs) != 0 {
  35. return cors.New(configs[0])
  36. }
  37. return cors.New(defaultCORSConfig)
  38. }