github.com/apipluspower/gqlgen@v0.15.2/graphql/playground/playground.go (about)

     1  package playground
     2  
     3  import (
     4  	"html/template"
     5  	"net/http"
     6  )
     7  
     8  var page = template.Must(template.New("graphiql").Parse(`<!DOCTYPE html>
     9  <html>
    10    <head>
    11      <title>{{.title}}</title>
    12      <link
    13  		rel="stylesheet"
    14  		href="https://cdn.jsdelivr.net/npm/graphiql@{{.version}}/graphiql.min.css"
    15  		integrity="{{.cssSRI}}"
    16  		crossorigin="anonymous"
    17  	/>
    18    </head>
    19    <body style="margin: 0;">
    20      <div id="graphiql" style="height: 100vh;"></div>
    21  
    22  	<script
    23  		src="https://cdn.jsdelivr.net/npm/react@17.0.2/umd/react.production.min.js"
    24  		integrity="{{.reactSRI}}"
    25  		crossorigin="anonymous"
    26  	></script>
    27  	<script
    28  		src="https://cdn.jsdelivr.net/npm/react-dom@17.0.2/umd/react-dom.production.min.js"
    29  		integrity="{{.reactDOMSRI}}"
    30  		crossorigin="anonymous"
    31  	></script>
    32  	<script
    33  		src="https://cdn.jsdelivr.net/npm/graphiql@{{.version}}/graphiql.min.js"
    34  		integrity="{{.jsSRI}}"
    35  		crossorigin="anonymous"
    36  	></script>
    37  
    38      <script>
    39        const url = location.protocol + '//' + location.host + '{{.endpoint}}';
    40        const wsProto = location.protocol == 'https:' ? 'wss:' : 'ws:';
    41        const subscriptionUrl = wsProto + '//' + location.host + '{{.endpoint}}';
    42  
    43        const fetcher = GraphiQL.createFetcher({ url, subscriptionUrl });
    44  
    45        ReactDOM.render(
    46          React.createElement(GraphiQL, { fetcher: fetcher }),
    47          document.getElementById('graphiql'),
    48        );
    49      </script>
    50    </body>
    51  </html>
    52  `))
    53  
    54  func Handler(title string, endpoint string) http.HandlerFunc {
    55  	return func(w http.ResponseWriter, r *http.Request) {
    56  		w.Header().Add("Content-Type", "text/html")
    57  		err := page.Execute(w, map[string]string{
    58  			"title":       title,
    59  			"endpoint":    endpoint,
    60  			"version":     "1.5.16",
    61  			"cssSRI":      "sha256-HADQowUuFum02+Ckkv5Yu5ygRoLllHZqg0TFZXY7NHI=",
    62  			"jsSRI":       "sha256-uHp12yvpXC4PC9+6JmITxKuLYwjlW9crq9ywPE5Rxco=",
    63  			"reactSRI":    "sha256-Ipu/TQ50iCCVZBUsZyNJfxrDk0E2yhaEIz0vqI+kFG8=",
    64  			"reactDOMSRI": "sha256-nbMykgB6tsOFJ7OdVmPpdqMFVk4ZsqWocT6issAPUF0=",
    65  		})
    66  		if err != nil {
    67  			panic(err)
    68  		}
    69  	}
    70  }