package graphiql import ( "html/template" "net/http" "net/url" ) var page = template.Must(template.New("graphiql").Parse(` {{.title}}
Loading...
`)) // Handler responsible for setting up the playground func Handler(title string, endpoint string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "text/html; charset=UTF-8") err := page.Execute(w, map[string]interface{}{ "title": title, "endpoint": endpoint, "endpointIsAbsolute": endpointHasScheme(endpoint), "subscriptionEndpoint": getSubscriptionEndpoint(endpoint), "version": "2.0.10", "cssSRI": "sha256-gQryfbGYeYFxnJYnfPStPYFt0+uv8RP8Dm++eh00G9c=", "jsSRI": "sha256-qQ6pw7LwTLC+GfzN+cJsYXfVWRKH9O5o7+5H96gTJhQ=", "reactSRI": "sha256-Ipu/TQ50iCCVZBUsZyNJfxrDk0E2yhaEIz0vqI+kFG8=", "reactDOMSRI": "sha256-nbMykgB6tsOFJ7OdVmPpdqMFVk4ZsqWocT6issAPUF0=", }) if err != nil { panic(err) } } } // endpointHasScheme checks if the endpoint has a scheme. func endpointHasScheme(endpoint string) bool { u, err := url.Parse(endpoint) return err == nil && u.Scheme != "" } // getSubscriptionEndpoint returns the subscription endpoint for the given // endpoint if it is parsable as a URL, or an empty string. func getSubscriptionEndpoint(endpoint string) string { u, err := url.Parse(endpoint) if err != nil { return "" } switch u.Scheme { case "https": u.Scheme = "wss" default: u.Scheme = "ws" } return u.String() }