gqlgen拒绝来自http://localhost:3000的连接的CORS请求。

huangapple go评论76阅读模式
英文:

gqlgen CORS refusing connection from http://localhost:3000

问题

我正在尝试使用官方文档中的代码为我的gqlgen服务器添加CORS,但是当我尝试从http://localhost:3000连接到服务器时,连接被拒绝。

package main

import (
	"log"
	"net/http"
	"os"

	"github.com/99designs/gqlgen/graphql/handler"
	"github.com/99designs/gqlgen/graphql/handler/transport"
	"github.com/99designs/gqlgen/graphql/playground"
	"github.com/go-chi/chi"
	"github.com/gorilla/websocket"
	"github.com/megajon/gqlgen-cors/graph"
	"github.com/rs/cors"
)

const defaultPort = "8081"

func main() {
	port := os.Getenv("PORT")
	if port == "" {
		port = defaultPort
	}

	router := chi.NewRouter()

	// 在每个请求周围添加CORS中间件
	// 有关完整选项列表,请参见https://github.com/rs/cors
	router.Use(cors.New(cors.Options{
		AllowedOrigins:   []string{"http://localhost:3000"},
		AllowCredentials: true,
		Debug:            true,
	}).Handler)

	srv := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &graph.Resolver{}}))
	srv.AddTransport(&transport.Websocket{
		Upgrader: websocket.Upgrader{
			CheckOrigin: func(r *http.Request) bool {
				// 在这里检查您想要的域名
				return r.Host == "example.org"
			},
			ReadBufferSize:  1024,
			WriteBufferSize: 1024,
		},
	})

	router.Handle("/", playground.Handler("GraphQL playground", "/query"))
	router.Handle("/query", srv)

	log.Printf("连接到 http://localhost:%s/ 查看GraphQL playground", port)
	err := http.ListenAndServe(":"+port, router)
	if err != nil {
		panic(err)
	}
}

这是你要翻译的内容。

英文:

I'm trying to add CORS to my gqlgen server using the code from the official docs, but when I try to connect to the server from http://localhost:3000 the connection is refused.

package main
import (
"log"
"net/http"
"os"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/go-chi/chi"
"github.com/gorilla/websocket"
"github.com/megajon/gqlgen-cors/graph"
"github.com/rs/cors"
)
const defaultPort = "8081"
func main() {
port := os.Getenv("PORT")
if port == "" {`your text`
port = defaultPort
}
router := chi.NewRouter()
// Add CORS middleware around every request
// See https://github.com/rs/cors for full option listing
router.Use(cors.New(cors.Options{
AllowedOrigins:   []string{"http://localhost:3000"},
AllowCredentials: true,
Debug:            true,
}).Handler)
srv := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &graph.Resolver{}}))
srv.AddTransport(&transport.Websocket{
Upgrader: websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
// Check against your desired domains here
return r.Host == "example.org"
},
ReadBufferSize:  1024,
WriteBufferSize: 1024,
},
})
router.Handle("/", playground.Handler("GraphQL playground", "/query"))
router.Handle("/query", srv)
log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
err := http.ListenAndServe(":"+port, router)
if err != nil {
panic(err)
}
}

答案1

得分: 1

原来问题不是CORS,而是gqlgen API运行在Windows Powershell上,而前端应用程序运行在wsl ubuntu终端上。这实际上是两个独立的网络。一旦我在相同的环境中运行两个应用程序,问题就解决了。

英文:

It turns out the issue wasn't CORS, but the fact that the gqlgen API was running from a Windows Powershell and the front end app was running from a wsl ubuntu terminal. Which is essentially two separate networks. Once I ran both apps in the same environment the problem was resolved.

huangapple
  • 本文由 发表于 2023年3月18日 00:25:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75769976.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定