另客网go项目公用的代码库

cors.go 703B

1234567891011121314151617181920212223242526272829303132
  1. package cors
  2. import (
  3. "net/http"
  4. "strings"
  5. "github.com/gin-contrib/cors"
  6. "github.com/gin-gonic/gin"
  7. )
  8. func Cors() gin.HandlerFunc {
  9. config := cors.DefaultConfig()
  10. config.AddAllowMethods(http.MethodDelete, http.MethodOptions, http.MethodPatch)
  11. // x-requested-with antd上传组件需要
  12. config.AddAllowHeaders("Authorization", "X-Require-Cookie", "X-Device", "x-requested-with", "X-Tourist-Id")
  13. config.AllowCredentials = true
  14. config.AllowOriginFunc = func(origin string) bool {
  15. if gin.Mode() == gin.DebugMode {
  16. return true
  17. }
  18. if strings.Contains(origin, "bilingo.com") || strings.Contains(origin, "links123.com") {
  19. return true
  20. }
  21. return false
  22. }
  23. return cors.New(config)
  24. }