package cors import ( "net/http" "strings" log "github.com/sirupsen/logrus" ) func AllowCors(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if origin := r.Header.Get("Origin"); origin != "" { log.Infof("Setting CORS header: %v", origin) w.Header().Set("Access-Control-Allow-Origin", origin) if r.Method == "OPTIONS" && r.Header.Get("Access-Control-Request-Method") != "" { preflightHandler(w, r) return } } h.ServeHTTP(w, r) }) } func preflightHandler(w http.ResponseWriter, r *http.Request) { log.Infof("preflight request for %s", r.URL.Path) headers := []string{"Content-Type", "Accept"} w.Header().Set("Access-Control-Allow-Headers", strings.Join(headers, ",")) methods := []string{"GET", "HEAD", "POST", "PUT", "DELETE"} w.Header().Set("Access-Control-Allow-Methods", strings.Join(methods, ",")) }