Skip to content

Instantly share code, notes, and snippets.

@adell
Last active January 14, 2022 21:12
Show Gist options
  • Save adell/f26d74376e587cac8f72fb6d12a30235 to your computer and use it in GitHub Desktop.
Save adell/f26d74376e587cac8f72fb6d12a30235 to your computer and use it in GitHub Desktop.
Tentando habilitar cors em um projeto gin
package routes
import (
"time"
cors "github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/hyperyuri/webapi-with-go/controllers"
)
func ConfigRoutes(router *gin.Engine) *gin.Engine {
router.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET", "HEAD", "OPTIONS", "POST", "PUT", "PATCH", "DELETE"},
AllowHeaders: []string{"Origin", "Authorization", "Cookie", "Content-Type"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
AllowOriginFunc: func(origin string) bool {
return origin == "*"
},
MaxAge: 12 * time.Hour,
}))
router.Use(cors.Default())
router.GET("/api/v1/books", controllers.ShowAllBooks)
return router
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment