Skip to content

Instantly share code, notes, and snippets.

@vcostin
Last active May 27, 2019 12:35
Show Gist options
  • Save vcostin/a82a5bc6c33cd8d712974af3b89f8cfb to your computer and use it in GitHub Desktop.
Save vcostin/a82a5bc6c33cd8d712974af3b89f8cfb to your computer and use it in GitHub Desktop.
GO http CORS middleware
package middleware
import (
"net/http"
)
// Cors hello
func Cors(next http.Handler) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "*")
w.Header().Set("Access-Control-Allow-Headers", "*")
if r.Method == http.MethodOptions {
return
}
next.ServeHTTP(w, r)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment