Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions router/internal/graphiql/playgroundhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,28 @@ type PlaygroundOptions struct {
}

type Playground struct {
next http.Handler
opts *PlaygroundOptions
next http.Handler
opts *PlaygroundOptions
templateBytes *[]byte
Comment thread
df-wg marked this conversation as resolved.
Outdated
}

func NewPlayground(opts *PlaygroundOptions) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return &Playground{
p := &Playground{
next: next,
opts: opts,
}
p.initPlayground()
return p
}
}

func (p *Playground) initPlayground() {
tpl := strings.Replace(p.opts.Html, "{{graphqlURL}}", p.opts.GraphqlURL, -1)
play := []byte(tpl)
p.templateBytes = &play
}

func (p *Playground) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Only serve the playground if the request is for text/html
// if not, just pass through to the next handler
Expand All @@ -35,12 +44,9 @@ func (p *Playground) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

tpl := strings.Replace(p.opts.Html, "{{graphqlURL}}", p.opts.GraphqlURL, -1)
resp := []byte(tpl)

w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Content-Length", strconv.Itoa(len(resp)))
w.Header().Set("Content-Length", strconv.Itoa(len(*p.templateBytes)))

w.WriteHeader(http.StatusOK)
_, _ = w.Write(resp)
_, _ = w.Write(*p.templateBytes)
Comment thread
df-wg marked this conversation as resolved.
Outdated
}