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

cors.go 568B

12345678910111213141516171819202122232425262728293031
  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. config.AddAllowHeaders("Authorization", "X-Require-Cookie")
  12. config.AllowCredentials = true
  13. config.AllowOriginFunc = func(origin string) bool {
  14. if gin.Mode() == gin.DebugMode {
  15. return true
  16. }
  17. if strings.Contains(origin, "links123.com") {
  18. return true
  19. }
  20. return false
  21. }
  22. return cors.New(config)
  23. }