(handler http.HandlerFunc)
| 3 | import "net/http" |
| 4 | |
| 5 | func enableCORS(handler http.HandlerFunc) http.HandlerFunc { |
| 6 | return func(w http.ResponseWriter, r *http.Request) { |
| 7 | w.Header().Set("Access-Control-Allow-Origin", "*") |
| 8 | w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") |
| 9 | w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization") |
| 10 | |
| 11 | // allow preflight requests from the browser API |
| 12 | if r.Method == "OPTIONS" { |
| 13 | w.WriteHeader(http.StatusOK) |
| 14 | return |
| 15 | } |
| 16 | |
| 17 | handler(w, r) |
| 18 | } |
| 19 | } |