-3

my project uses go-gin, and I tried setting cors

When I submitted the following code,

package middleware

import (
    "github.com/gin-contrib/cors"
    "github.com/gin-gonic/gin"
)

func Use() {
    gin.SetMode(gin.ReleaseMode)

    cors.Default()
    return
}

func main() {
    log.Printf("Server started")

    r := gin.Default()

    route.Route(r)

    middleware.Use()

    log.Fatal(r.Run(":8080"))
}

it was pointed out that cors did not work, and this method worked fine with the application I created before, but I do not know what the problem is with this application

David Buck
  • 3,752
  • 35
  • 31
  • 35
jadejoe
  • 663
  • 2
  • 13
  • 24
  • 2
    You never passed the cors middleware to the router. See the cors package readme for a full example: https://github.com/gin-contrib/cors#canonical-example – Adrian Oct 24 '19 at 16:54

1 Answers1

1

You are not setting cors Default in your router correctly

check this basic example from the docs

func main() {
router := gin.Default()
router.Use(cors.Default()) // <- you are missing this step
router.Run()
}

The middleware.Use() that you do, doesn't set the cors to your router

Check this github docs page for more info about the topic

stackr
  • 2,742
  • 27
  • 44
  • thanks!! i missed setting so change code ``` func main() { log.Printf("Server started") r := gin.Default() r.Use(middleware,Use()) route.Route(r) log.Fatal(r.Run(":8080")) } ``` Also, thanks to cors being placed and set in front of route to reflect the influence of CORS on the router – jadejoe Oct 27 '19 at 15:48