Skip to content

Commit 90c28fa

Browse files
authored
Add configurable playground HTTP handler (#169)
Adds a new HTTP handler that serves a configurable playground UI. Unlike the existing `gateway.PlaygroundHandler()` method, this one serves the static UI only and allows for some critical settings like request headers. Also updates the version of the playground to the latest. The cmd/gateway updates make it easier to use for the first time (don't need to remember to visit `/graphql`) and doesn't negatively impact previous users. Closes #158 Closes #120
1 parent 88533dc commit 90c28fa

File tree

4 files changed

+168
-501
lines changed

4 files changed

+168
-501
lines changed

cmd/gateway/http.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"net/http"
66
"os"
7+
"strings"
78

89
"github.com/nautilus/gateway"
910
"github.com/nautilus/graphql"
@@ -25,31 +26,44 @@ func ListenAndServe(services []string) {
2526
}
2627

2728
// add the graphql endpoints to the router
28-
http.HandleFunc("/graphql", setCORSHeaders(gw.PlaygroundHandler))
29+
http.HandleFunc("/graphql", func(w http.ResponseWriter, r *http.Request) {
30+
if r.Method == http.MethodGet && strings.Contains(r.Header.Get("Accept"), "text/html") { // rudimentary check to see if this is accessed from a browser UI
31+
// if calling from a UI, redirect to the UI handler
32+
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
33+
return
34+
}
35+
gw.GraphQLHandler(w, r)
36+
})
2937

30-
// start the server
31-
fmt.Printf("🚀 Gateway is ready at http://localhost:%s/graphql\n", Port)
32-
err = http.ListenAndServe(fmt.Sprintf(":%s", Port), nil)
33-
if err != nil {
34-
fmt.Println(err.Error())
35-
os.Exit(1)
36-
}
37-
}
38+
playgroundHandler := gw.StaticPlaygroundHandler(gateway.PlaygroundConfig{
39+
Endpoint: "/graphql",
40+
})
41+
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
42+
if r.URL.Path != "/" {
43+
// ensure our catch-all handler pattern "/" only runs on "/"
44+
http.NotFound(w, r)
45+
return
46+
}
3847

39-
func setCORSHeaders(fn http.HandlerFunc) http.HandlerFunc {
40-
return func(w http.ResponseWriter, req *http.Request) {
4148
// set the necessary CORS headers
4249
w.Header().Set("Access-Control-Allow-Origin", "*")
4350
w.Header().Set("Access-Control-Allow-Credentials", "true")
4451
w.Header().Set("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT")
4552
w.Header().Set("Access-Control-Allow-Headers", "*")
4653

4754
// if we are handling a pre-flight request
48-
if req.Method == http.MethodOptions {
55+
if r.Method == http.MethodOptions {
4956
return
5057
}
5158

52-
// invoke the handler
53-
fn(w, req)
59+
playgroundHandler.ServeHTTP(w, r)
60+
})
61+
62+
// start the server
63+
fmt.Printf("🚀 Gateway is ready at http://localhost:%s/graphql\n", Port)
64+
err = http.ListenAndServe(fmt.Sprintf(":%s", Port), nil)
65+
if err != nil {
66+
fmt.Println(err.Error())
67+
os.Exit(1)
5468
}
5569
}

0 commit comments

Comments
 (0)