Skip to content

Instantly share code, notes, and snippets.

@jtrein
Last active October 31, 2017 03:24
Show Gist options
  • Save jtrein/b5b5a0474d2f74073e00cf251a280461 to your computer and use it in GitHub Desktop.
Save jtrein/b5b5a0474d2f74073e00cf251a280461 to your computer and use it in GitHub Desktop.
CORS Header Golang (Gorilla)
// https://stackoverflow.com/questions/40985920/making-golang-gorilla-cors-handler-work/40987389#40987389
headersOk := handlers.AllowedHeaders([]string{"X-Requested-With"})
originsOk := handlers.AllowedOrigins([]string{os.Getenv("ORIGIN_ALLOWED")})
methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"})
// start server listen
// with error handling
log.Fatal(http.ListenAndServe(":" + os.Getenv("PORT"), handlers.CORS(originsOk, headersOk, methodsOk)(router)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment