github.com/HaswinVidanage/gqlgen@v0.8.1-0.20220609041233-69528c1bf712/handler/playground.go (about)

     1  package handler
     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  	<meta charset=utf-8/>
    12  	<meta name="viewport" content="user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui">
    13  	<link rel="shortcut icon" href="https://graphcool-playground.netlify.com/favicon.png">
    14  	<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/graphql-playground-react@{{ .version }}/build/static/css/index.css" 
    15  		integrity="{{ .cssSRI }}" crossorigin="anonymous"/>
    16  	<link rel="shortcut icon" href="//cdn.jsdelivr.net/npm/graphql-playground-react@{{ .version }}/build/favicon.png"
    17  		integrity="{{ .faviconSRI }}" crossorigin="anonymous"/>
    18  	<script src="//cdn.jsdelivr.net/npm/graphql-playground-react@{{ .version }}/build/static/js/middleware.js"
    19  		integrity="{{ .jsSRI }}" crossorigin="anonymous"></script>
    20  	<title>{{.title}}</title>
    21  </head>
    22  <body>
    23  <style type="text/css">
    24  	html { font-family: "Open Sans", sans-serif; overflow: hidden; }
    25  	body { margin: 0; background: #172a3a; }
    26  </style>
    27  <div id="root"/>
    28  <script type="text/javascript">
    29  	window.addEventListener('load', function (event) {
    30  		const root = document.getElementById('root');
    31  		root.classList.add('playgroundIn');
    32  		const wsProto = location.protocol == 'https:' ? 'wss:' : 'ws:'
    33  		GraphQLPlayground.init(root, {
    34  			endpoint: location.protocol + '//' + location.host + '{{.endpoint}}',
    35  			subscriptionsEndpoint: wsProto + '//' + location.host + '{{.endpoint }}',
    36  			settings: {
    37  				'request.credentials': 'same-origin'
    38  			}
    39  		})
    40  	})
    41  </script>
    42  </body>
    43  </html>
    44  `))
    45  
    46  func Playground(title string, endpoint string) http.HandlerFunc {
    47  	return func(w http.ResponseWriter, r *http.Request) {
    48  		w.Header().Add("Content-Type", "text/html")
    49  		err := page.Execute(w, map[string]string{
    50  			"title":      title,
    51  			"endpoint":   endpoint,
    52  			"version":    "1.7.8",
    53  			"cssSRI":     "sha256-cS9Vc2OBt9eUf4sykRWukeFYaInL29+myBmFDSa7F/U=",
    54  			"faviconSRI": "sha256-GhTyE+McTU79R4+pRO6ih+4TfsTOrpPwD8ReKFzb3PM=",
    55  			"jsSRI":      "sha256-ucQsC5k+XYnUlQia6tMKdAOGBbfbDAquMa+oqIooB5A=",
    56  		})
    57  		if err != nil {
    58  			panic(err)
    59  		}
    60  	}
    61  }