github.com/humans-group/gqlgen@v0.7.2/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  	<link rel="shortcut icon" href="//cdn.jsdelivr.net/npm/graphql-playground-react@{{ .version }}/build/favicon.png"/>
    16  	<script src="//cdn.jsdelivr.net/npm/graphql-playground-react@{{ .version }}/build/static/js/middleware.js"></script>
    17  	<title>{{.title}}</title>
    18  </head>
    19  <body>
    20  <style type="text/css">
    21  	html { font-family: "Open Sans", sans-serif; overflow: hidden; }
    22  	body { margin: 0; background: #172a3a; }
    23  </style>
    24  <div id="root"/>
    25  <script type="text/javascript">
    26  	window.addEventListener('load', function (event) {
    27  		const root = document.getElementById('root');
    28  		root.classList.add('playgroundIn');
    29  		const wsProto = location.protocol == 'https:' ? 'wss:' : 'ws:'
    30  		GraphQLPlayground.init(root, {
    31  			endpoint: location.protocol + '//' + location.host + '{{.endpoint}}',
    32  			subscriptionsEndpoint: wsProto + '//' + location.host + '{{.endpoint }}',
    33  			settings: {
    34  				'request.credentials': 'same-origin'
    35  			}
    36  		})
    37  	})
    38  </script>
    39  </body>
    40  </html>
    41  `))
    42  
    43  func Playground(title string, endpoint string) http.HandlerFunc {
    44  	return func(w http.ResponseWriter, r *http.Request) {
    45  		err := page.Execute(w, map[string]string{
    46  			"title":    title,
    47  			"endpoint": endpoint,
    48  			"version":  "1.7.8",
    49  		})
    50  		if err != nil {
    51  			panic(err)
    52  		}
    53  	}
    54  }