cors.go 512 B

123456789101112131415161718
  1. package middleware
  2. import (
  3. "net/http"
  4. )
  5. // Cors is a middleware to enabling CORS on HTTP requests
  6. func Cors(inner 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-Methods", "*")
  10. w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, Authorization")
  11. if r.Method != "OPTIONS" {
  12. inner.ServeHTTP(w, r)
  13. }
  14. })
  15. }