-
-
Save parzibyte/e7a4ced5286ed97fd21d3dd64a30a666 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func enableCORS(router *mux.Router) { | |
router.PathPrefix("/").HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | |
w.Header().Set("Access-Control-Allow-Origin", AllowedCORSDomain) | |
}).Methods(http.MethodOptions) | |
router.Use(middlewareCors) | |
} | |
func middlewareCors(next http.Handler) http.Handler { | |
return http.HandlerFunc( | |
func(w http.ResponseWriter, req *http.Request) { | |
// Just put some headers to allow CORS... | |
w.Header().Set("Access-Control-Allow-Origin", AllowedCORSDomain) | |
w.Header().Set("Access-Control-Allow-Credentials", "true") | |
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE") | |
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization") | |
// and call next handler! | |
next.ServeHTTP(w, req) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment