#go #gorilla #mux
#Вперед #горилла #мультиплексирование
Вопрос:
Я наткнулся на этот код и хотел бы разрешить CORS на портах 1111,1112 и 1113, как я могу это сделать?
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/all", All).Methods("GET")
router.HandleFunc("/gotoT", Goto).Methods("GET")
headers := handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type", "Authorization"})
methods := handlers.AllowedMethods([]string{"GET", "POST", "PUT", "HEAD", "OPTIONS"})
origins := handlers.AllowedOrigins([]string{"*"})
log.Fatal(http.ListenAndServe(":1111", handlers.CORS(headers, methods, origins)(router)))
В этом примере CORS прослушивает только порт 1111, поэтому я хотел бы добавить больше портов, пожалуйста, любая помощь?
Комментарии:
1. В этом примере http-сервер прослушивает порт 1111. Вы хотите, чтобы один и тот же сервер работал на разных портах одновременно? Возможно, переадресация портов — лучшая идея.
Ответ №1:
Поместите их в отдельные процедуры go:
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/all", All).Methods("GET")
router.HandleFunc("/gotoT", Goto).Methods("GET")
headers := handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type", "Authorization"})
methods := handlers.AllowedMethods([]string{"GET", "POST", "PUT", "HEAD", "OPTIONS"})
origins := handlers.AllowedOrigins([]string{"*"})
corsRouter := handlers.CORS(headers, methods, origins)(router)
go func() {
log.Fatal(http.ListenAndServe(":1111", corsRouter))
}()
go func() {
log.Fatal(http.ListenAndServe(":1112", corsRouter))
}()
log.Fatal(http.ListenAndServe(":1113", corsRouter))