cors.go 616 B

12345678910111213141516171819202122
  1. package util
  2. import (
  3. "net/http"
  4. )
  5. // Cors writes cors headers
  6. func Cors(h http.Handler) http.Handler {
  7. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  8. w.Header().Set("Access-Control-Allow-Origin", "*")
  9. w.Header().Set("Access-Control-Allow-Credentials", "true")
  10. w.Header().Set("Access-Control-Allow-Methods", "GET,POST,PATCH,PUT,DELETE,OPTIONS")
  11. w.Header().Set("Access-Control-Expose-Headers", "*")
  12. w.Header().Set("Access-Control-Allow-Headers", "*")
  13. if r.Method == "OPTIONS" {
  14. WriteResponse(w, http.StatusOK, "Ok", "Ok")
  15. return
  16. } else {
  17. h.ServeHTTP(w, r)
  18. }
  19. })
  20. }