playground.go

 1package handler
 2
 3import (
 4	"html/template"
 5	"net/http"
 6)
 7
 8var 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="https://cdn.jsdelivr.net/npm/graphql-playground-react@{{ .version }}/build/static/css/index.css" 
15		integrity="{{ .cssSRI }}" crossorigin="anonymous"/>
16	<link rel="shortcut icon" href="https://cdn.jsdelivr.net/npm/graphql-playground-react@{{ .version }}/build/favicon.png"
17		integrity="{{ .faviconSRI }}" crossorigin="anonymous"/>
18	<script src="https://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
46func 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.20",
53			"cssSRI":     "sha256-cS9Vc2OBt9eUf4sykRWukeFYaInL29+myBmFDSa7F/U=",
54			"faviconSRI": "sha256-GhTyE+McTU79R4+pRO6ih+4TfsTOrpPwD8ReKFzb3PM=",
55			"jsSRI":      "sha256-4QG1Uza2GgGdlBL3RCBCGtGeZB6bDbsw8OltCMGeJsA=",
56		})
57		if err != nil {
58			panic(err)
59		}
60	}
61}