package main import ( "github.com/gorilla/mux" "github.com/rs/cors" "net/http" "rate-it-api/controllers" "strings" ) // APIRouter handle routes func APIRouter() *mux.Router { router := mux.NewRouter().StrictSlash(true) c := cors.New(cors.Options{ AllowedOrigins: []string{"*"}, AllowCredentials: true, AllowedHeaders: strings.Split("Access-Control-Allow-Methods,Access-Control-Allow-Origin,Content-Type", ","), ExposedHeaders: strings.Split("*", ","), OptionsPassthrough: true, Debug: false, }) for _, route := range routes { var handler http.Handler handler = route.HandlerFunc handler = Logger(handler, route.Name) handler = c.Handler(handler) // route.Method += ",OPTIONS" router. Methods(strings.Split(route.Method, ",")...). Path(route.Pattern). Name(route.Name). Handler(handler) } for _, route := range routesJwt { var handler http.Handler handler = route.HandlerFunc handler = cors.Default().Handler(handler) handler = Logger(handler, route.Name) handler = controllers.JwtMiddleware.Handler(handler) route.Method += ",OPTIONS" router. Methods(strings.Split(route.Method, ",")...). Path(route.Pattern). Name(route.Name). Handler(handler) } return router }