github.com/senomas/gqlgen@v0.17.11-0.20220626120754-9aee61b0716a/graphql/playground/playground.go (about)

     1  package playground
     2  
     3  import (
     4  	"html/template"
     5  	"net/http"
     6  	"net/url"
     7  )
     8  
     9  var page = template.Must(template.New("graphiql").Parse(`<!DOCTYPE html>
    10  <html>
    11    <head>
    12      <title>{{.title}}</title>
    13      <link
    14  		rel="stylesheet"
    15  		href="https://cdn.jsdelivr.net/npm/graphiql@{{.version}}/graphiql.min.css"
    16  		integrity="{{.cssSRI}}"
    17  		crossorigin="anonymous"
    18  	/>
    19    </head>
    20    <body style="margin: 0;">
    21      <div id="graphiql" style="height: 100vh;"></div>
    22  
    23  	<script
    24  		src="https://cdn.jsdelivr.net/npm/react@17.0.2/umd/react.production.min.js"
    25  		integrity="{{.reactSRI}}"
    26  		crossorigin="anonymous"
    27  	></script>
    28  	<script
    29  		src="https://cdn.jsdelivr.net/npm/react-dom@17.0.2/umd/react-dom.production.min.js"
    30  		integrity="{{.reactDOMSRI}}"
    31  		crossorigin="anonymous"
    32  	></script>
    33  	<script
    34  		src="https://cdn.jsdelivr.net/npm/graphiql@{{.version}}/graphiql.min.js"
    35  		integrity="{{.jsSRI}}"
    36  		crossorigin="anonymous"
    37  	></script>
    38  
    39      <script>
    40  {{- if .endpointIsAbsolute}}
    41        const url = {{.endpoint}};
    42        const subscriptionUrl = {{.subscriptionEndpoint}};
    43  {{- else}}
    44        const url = location.protocol + '//' + location.host + {{.endpoint}};
    45        const wsProto = location.protocol == 'https:' ? 'wss:' : 'ws:';
    46        const subscriptionUrl = wsProto + '//' + location.host + {{.endpoint}};
    47  {{- end}}
    48  
    49        const fetcher = GraphiQL.createFetcher({ url, subscriptionUrl });
    50        ReactDOM.render(
    51          React.createElement(GraphiQL, {
    52            fetcher: fetcher,
    53            tabs: true,
    54            headerEditorEnabled: true,
    55            shouldPersistHeaders: true
    56          }),
    57          document.getElementById('graphiql'),
    58        );
    59      </script>
    60    </body>
    61  </html>
    62  `))
    63  
    64  // Handler responsible for setting up the playground
    65  func Handler(title string, endpoint string) http.HandlerFunc {
    66  	return func(w http.ResponseWriter, r *http.Request) {
    67  		w.Header().Add("Content-Type", "text/html")
    68  		err := page.Execute(w, map[string]interface{}{
    69  			"title":                title,
    70  			"endpoint":             endpoint,
    71  			"endpointIsAbsolute":   endpointHasScheme(endpoint),
    72  			"subscriptionEndpoint": getSubscriptionEndpoint(endpoint),
    73  			"version":              "1.8.2",
    74  			"cssSRI":               "sha256-CDHiHbYkDSUc3+DS2TU89I9e2W3sJRUOqSmp7JC+LBw=",
    75  			"jsSRI":                "sha256-X8vqrqZ6Rvvoq4tvRVM3LoMZCQH8jwW92tnX0iPiHPc=",
    76  			"reactSRI":             "sha256-Ipu/TQ50iCCVZBUsZyNJfxrDk0E2yhaEIz0vqI+kFG8=",
    77  			"reactDOMSRI":          "sha256-nbMykgB6tsOFJ7OdVmPpdqMFVk4ZsqWocT6issAPUF0=",
    78  		})
    79  		if err != nil {
    80  			panic(err)
    81  		}
    82  	}
    83  }
    84  
    85  // endpointHasScheme checks if the endpoint has a scheme.
    86  func endpointHasScheme(endpoint string) bool {
    87  	u, err := url.Parse(endpoint)
    88  	return err == nil && u.Scheme != ""
    89  }
    90  
    91  // getSubscriptionEndpoint returns the subscription endpoint for the given
    92  // endpoint if it is parsable as a URL, or an empty string.
    93  func getSubscriptionEndpoint(endpoint string) string {
    94  	u, err := url.Parse(endpoint)
    95  	if err != nil {
    96  		return ""
    97  	}
    98  
    99  	switch u.Scheme {
   100  	case "https":
   101  		u.Scheme = "wss"
   102  	default:
   103  		u.Scheme = "ws"
   104  	}
   105  
   106  	return u.String()
   107  }