Making golang Gorilla CORS handler work

后端 未结 7 1308
轮回少年
轮回少年 2020-12-14 01:13

I have fairly simple setup here as described in the code below. But I am not able to get the CORS to work. I keep getting this error:

XML

7条回答
  •  遥遥无期
    2020-12-14 01:27

    package main
    
    import (
        "log"
        "net/http"
    
        "github.com/gorilla/handlers"
        "github.com/gorilla/mux"
        "myApp/src/controllers"
           "github.com/rs/cors"
    )
    
    func main() {
    
         ac := new(controllers.AccountController)
    
        router := mux.NewRouter()
        router.HandleFunc("/signup", ac.SignUp).Methods("POST")
        router.HandleFunc("/signin", ac.SignIn).Methods("POST")
    //cors optionsGoes Below
    corsOpts := cors.New(cors.Options{
        AllowedOrigins: []string{"http://localhost:8100"}, //you service is available and allowed for this base url 
        AllowedMethods: []string{
            http.MethodGet,//http methods for your app
            http.MethodPost,
            http.MethodPut,
            http.MethodPatch,
            http.MethodDelete,
            http.MethodOptions,
            http.MethodHead,
        },
    
        AllowedHeaders: []string{
            "*",//or you can your header key values which you are using in your application
    
        },
    })
    
        http.ListenAndServe(":3000", corsOpts.Handler(router))
    }
    

提交回复
热议问题