Last active
January 14, 2022 21:12
-
-
Save adell/f26d74376e587cac8f72fb6d12a30235 to your computer and use it in GitHub Desktop.
Tentando habilitar cors em um projeto gin
This file contains hidden or 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
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