1 回答
TA贡献1719条经验 获得超6个赞
当然可以。它(https://github.com/gin-contrib/cors)只是一个中间件。
package main
import (
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
// CORS for example.com and example.net origins
router.Use(cors.New(cors.Config{
AllowOrigins: []string{"example.com"},
AllowOriginFunc: func(origin string) bool {
return origin == "example.net"
}})).GET("/scrape", func(c *gin.Context) {
// serve something
})
allOrigins := router.Group("/")
allOrigins.Use(cors.Default())
allOrigins.GET("/data", func(c *gin.Context) {
// serve something
})
allOrigins.GET("/ping", func(c *gin.Context) {
// serve something
})
router.Run()
}
查看更多中间件示例:https ://github.com/gin-gonic/gin#using-middleware
- 1 回答
- 0 关注
- 81 浏览
添加回答
举报