cors.go 733 B

12345678910111213141516171819202122232425
  1. package middleware
  2. import (
  3. "git.sxidc.com/go-tools/api_binding/http_binding/binding_context"
  4. "net/http"
  5. )
  6. func Cors() Func {
  7. return func(c *binding_context.Context) {
  8. method := c.Request.Method
  9. c.Header("Access-Control-Allow-Origin", "*")
  10. c.Header("Access-Control-Allow-Headers", "Content-Length, Content-Type, AccessToken, X-CSRF-Token, Authorization, Token")
  11. c.Header("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE,OPTIONS")
  12. c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, "+
  13. "Access-Control-Allow-Headers, Content-Type")
  14. c.Header("Access-Control-Allow-Credentials", "true")
  15. if method == "OPTIONS" {
  16. c.AbortWithStatus(http.StatusOK)
  17. }
  18. c.Next()
  19. }
  20. }