Skip to content

Instantly share code, notes, and snippets.

@konojunya
Created July 17, 2018 12:09
Show Gist options
  • Save konojunya/a86a211a66f63a51a9ec1d65e060afcc to your computer and use it in GitHub Desktop.
Save konojunya/a86a211a66f63a51a9ec1d65e060afcc to your computer and use it in GitHub Desktop.
cors golang
r := gin.Default()
r.Use(cros)
func cros(c *gin.Context) {
headers := c.Request.Header.Get("Access-Control-Request-Headers")
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Methods", "GET,POST,PUT,HEAD,PATCH,DELETE,OPTIONS")
c.Header("Access-Control-Allow-Headers", headers)
if c.Request.Method == "OPTIONS" {
c.Status(200)
c.Abort()
}
c.Set("start_time", time.Now())
c.Next()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment