在Golang中启用CORS策略。

huangapple go评论127阅读模式
英文:

Enable Cors policy in golang

问题

在使用Golang的服务器端如何启用CORS策略?

你可以使用以下代码在Golang中实现CORS策略:

  1. func CorsMiddleware() gin.HandlerFunc {
  2. return func(c *gin.Context) {
  3. c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
  4. c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
  5. c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type,access-control-allow-origin, access-control-allow-headers")
  6. c.Next()
  7. }
  8. }
  9. func main() {
  10. defer config.CloseDatabaseConnection(db)
  11. r := gin.Default()
  12. r.Use(CorsMiddleware()) // 添加CORS中间件
  13. dataRoutes := r.Group("api/item")
  14. {
  15. dataRoutes.GET("/", dataController.All)
  16. dataRoutes.POST("/", dataController.Insert)
  17. dataRoutes.GET("/:id", dataController.FindByID)
  18. dataRoutes.PUT("/:id", dataController.Update)
  19. dataRoutes.DELETE("/:id", dataController.Delete)
  20. }
  21. r.Run()
  22. }

在上面的代码中,我们定义了一个名为CorsMiddleware的中间件函数,该函数设置了CORS相关的响应头。然后,在main函数中使用r.Use(CorsMiddleware())将该中间件应用到所有的路由上。

这样就可以在Golang中实现CORS策略了。希望对你有帮助!

英文:

How do we enable cors policy in server side using golang ?
main.go

  1. func main() {
  2. defer config.CloseDatabaseConnection(db)
  3. r := gin.Default()
  4. dataRoutes := r.Group("api/item")
  5. {
  6. dataRoutes.GET("/", dataController.All)
  7. dataRoutes.POST("/", dataController.Insert)
  8. dataRoutes.GET("/:id", dataController.FindByID)
  9. dataRoutes.PUT("/:id", dataController.Update)
  10. dataRoutes.DELETE("/:id", dataController.Delete)
  11. }
  12. r.Run()
  13. }

I found

  1. func Cors(w http.ResponseWriter, r *http.Request) {
  2. w.Header().Set("Content-Type", "text/html; charset=ascii")
  3. w.Header().Set("Access-Control-Allow-Origin", "*")
  4. w.Header().Set("Access-Control-Allow-Headers","Content-Type,access-control-allow-origin, access-control-allow-headers")
  5. }

But I am not sure how we implement in the golang? I have use above code while doing with c# but I am stuck in golang while implementing it.

答案1

得分: 1

如果你正在使用gin-gonic,你可以像下面的示例中所示,使用CORS中间件

如果你需要设置其他CORS特定的头部信息,请参考cors.Config的文档。

  1. import (
  2. // your other imports ...
  3. "github.com/gin-contrib/cors"
  4. "github.com/gin-gonic/gin"
  5. )
  6. func main() {
  7. defer config.CloseDatabaseConnection(db)
  8. r := gin.Default()
  9. r.Use(cors.New(cors.Config{
  10. AllowOrigins: []string{"*"},
  11. AllowMethods: []string{"POST", "PUT", "PATCH", "DELETE"},
  12. AllowHeaders: []string{"Content-Type,access-control-allow-origin, access-control-allow-headers"},
  13. }))
  14. dataRoutes := r.Group("api/item")
  15. {
  16. dataRoutes.GET("/", dataController.All)
  17. dataRoutes.POST("/", dataController.Insert)
  18. dataRoutes.GET("/:id", dataController.FindByID)
  19. dataRoutes.PUT("/:id", dataController.Update)
  20. dataRoutes.DELETE("/:id", dataController.Delete)
  21. }
  22. r.Run()
  23. }
英文:

If you are using gin-gonic then you can use the CORS middleware as shown in the example below.

If you need to set other CORS specific headers, see the documentation on cors.Config.

  1. import (
  2. // your other imports ...
  3. "github.com/gin-contrib/cors"
  4. "github.com/gin-gonic/gin"
  5. )
  6. func main() {
  7. defer config.CloseDatabaseConnection(db)
  8. r := gin.Default()
  9. r.Use(cors.New(cors.Config{
  10. AllowOrigins: []string{"*"},
  11. AllowMethods: []string{"POST", "PUT", "PATCH", "DELETE"},
  12. AllowHeaders: []string{"Content-Type,access-control-allow-origin, access-control-allow-headers"},
  13. }))
  14. dataRoutes := r.Group("api/item")
  15. {
  16. dataRoutes.GET("/", dataController.All)
  17. dataRoutes.POST("/", dataController.Insert)
  18. dataRoutes.GET("/:id", dataController.FindByID)
  19. dataRoutes.PUT("/:id", dataController.Update)
  20. dataRoutes.DELETE("/:id", dataController.Delete)
  21. }
  22. r.Run()
  23. }

huangapple
  • 本文由 发表于 2021年10月18日 04:56:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/69608544.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定