以下是我运行的代码,问题是我得到404页面没有找到而不是graphql游乐场页面是否可以使用gqlgen的httprouter,或者我需要回到chi或mux我也无法使用中间件,因为r没有使用方法package mainimport ( "gographql-server/graph" "gographql-server/graph/generated" "log" "net/http" "os" "github.com/99designs/gqlgen/graphql/handler" "github.com/99designs/gqlgen/graphql/playground" "github.com/julienschmidt/httprouter")const defaultPort = "8080"func PlaygroundHandler() httprouter.Handle { h := playground.Handler("GraphQL", "/query") return func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { h.ServeHTTP(w, req) }}func GraphqlHandler() httprouter.Handle { h := handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{Resolvers: &graph.Resolver{}})) return func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { h.ServeHTTP(w, req) }}func main() { port := os.Getenv("PORT") if port == "" { port = defaultPort } r := httprouter.New() r.POST("/query", GraphqlHandler()) r.GET("/", PlaygroundHandler()) // r.Use(middleware.RequestID) // r.Use(middleware.Logger) // r.Use(middleware.Recoverer) // r.Use(middlewares.AuthMiddleware()) log.Printf("connect to http://localhost:%s/ for GraphQL playground", port) log.Fatal(http.ListenAndServe(":"+port, nil))}
1 回答
HUWWW
TA贡献1874条经验 获得超12个赞
如果你传递到网址。听并服务()
在这条行中:nil
log.Fatal(http.ListenAndServe(":"+port, nil))
这意味着使用包的默认服务器。
但是您不会在默认的多路复用器中注册任何处理程序,而是使用 httprouter 创建自己的路由器。New()
,并在其中注册处理程序。net/http
因此,您必须将此路由器传递给:ListenAndServe()
log.Fatal(http.ListenAndServe(":"+port, r))
- 1 回答
- 0 关注
- 68 浏览
添加回答
举报
0/150
提交
取消