github.com/daeglee/go-ethereum@v0.0.0-20190504220456-cad3e8d18e9b/graphql/graphiql.go (about) 1 // The MIT License (MIT) 2 // 3 // Copyright (c) 2016 Muhammed Thanish 4 // 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 // 12 // The above copyright notice and this permission notice shall be included in all 13 // copies or substantial portions of the Software. 14 // 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 // SOFTWARE. 22 23 package graphql 24 25 import ( 26 "bytes" 27 "fmt" 28 "net/http" 29 ) 30 31 // GraphiQL is an in-browser IDE for exploring GraphiQL APIs. 32 // This handler returns GraphiQL when requested. 33 // 34 // For more information, see https://github.com/graphql/graphiql. 35 type GraphiQL struct{} 36 37 func respond(w http.ResponseWriter, body []byte, code int) { 38 w.Header().Set("Content-Type", "application/json; charset=utf-8") 39 w.Header().Set("X-Content-Type-Options", "nosniff") 40 w.WriteHeader(code) 41 _, _ = w.Write(body) 42 } 43 44 func errorJSON(msg string) []byte { 45 buf := bytes.Buffer{} 46 fmt.Fprintf(&buf, `{"error": "%s"}`, msg) 47 return buf.Bytes() 48 } 49 50 func (h GraphiQL) ServeHTTP(w http.ResponseWriter, r *http.Request) { 51 if r.Method != "GET" { 52 respond(w, errorJSON("only GET requests are supported"), http.StatusMethodNotAllowed) 53 return 54 } 55 56 w.Write(graphiql) 57 } 58 59 var graphiql = []byte(` 60 <!DOCTYPE html> 61 <html> 62 <head> 63 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/graphiql/0.11.11/graphiql.css"/> 64 <script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/2.0.3/fetch.min.js"></script> 65 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.2.0/umd/react.production.min.js"></script> 66 <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.2.0/umd/react-dom.production.min.js"></script> 67 <script src="https://cdnjs.cloudflare.com/ajax/libs/graphiql/0.11.11/graphiql.min.js"></script> 68 </head> 69 <body style="width: 100%; height: 100%; margin: 0; overflow: hidden;"> 70 <div id="graphiql" style="height: 100vh;">Loading...</div> 71 <script> 72 function fetchGQL(params) { 73 return fetch("/graphql", { 74 method: "post", 75 body: JSON.stringify(params), 76 credentials: "include", 77 }).then(function (resp) { 78 return resp.text(); 79 }).then(function (body) { 80 try { 81 return JSON.parse(body); 82 } catch (error) { 83 return body; 84 } 85 }); 86 } 87 88 ReactDOM.render( 89 React.createElement(GraphiQL, {fetcher: fetchGQL}), 90 document.getElementById("graphiql") 91 ) 92 </script> 93 </body> 94 </html> 95 `)