vardius / go-api-boilerplate

Go Server/API boilerplate using best practices DDD CQRS ES gRPC
https://go-api-boilerplate.local
MIT License
917 stars 135 forks source link

CORS HandlePreflight not generating any headers #65

Closed mar1n3r0 closed 4 years ago

mar1n3r0 commented 4 years ago

Stumbled upon this one: cors-preflight

router.HandleFunc("/profile/tweets", ProfileTweets).Methods("GET","OPTIONS")

c := cors.New(cors.Options{
    AllowedMethods: []string{"GET","POST", "OPTIONS"},
    AllowedOrigins: []string{"*"},
    AllowCredentials: true,
    AllowedHeaders: []string{"Content-Type","Bearer","Bearer ","content-type","Origin","Accept"},
    OptionsPassthrough: true,
})

Tried to replicate the solution with gorouter:

    // Public User routes
    router.POST("/v1/dispatch/{command}", commandDispatchHandler)
    // Handle preflight
    router.OPTIONS("/v1/dispatch/{command}", commandDispatchHandler)

Is there an option we can actually assign 2 methods to a single route at the same time ?

mar1n3r0 commented 4 years ago

Fixed, the solution was to add this to the handler and keep both POST and OPTIONS for the same route.

    // Stop here if its Preflighted OPTIONS request
    if r.Method == "OPTIONS" {
        return
    }