github.com/theQRL/go-zond@v0.1.1/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 "encoding/json" 27 "net/http" 28 "path/filepath" 29 30 "github.com/theQRL/go-zond/graphql/internal/graphiql" 31 "github.com/theQRL/go-zond/log" 32 ) 33 34 // GraphiQL is an in-browser IDE for exploring GraphiQL APIs. 35 // This handler returns GraphiQL when requested. 36 // 37 // For more information, see https://github.com/graphql/graphiql. 38 type GraphiQL struct{} 39 40 func respOk(w http.ResponseWriter, body []byte, ctype string) { 41 w.Header().Set("Content-Type", ctype) 42 w.Header().Set("X-Content-Type-Options", "nosniff") 43 w.Write(body) 44 } 45 46 func respErr(w http.ResponseWriter, msg string, code int) { 47 w.Header().Set("Content-Type", "application/json") 48 w.WriteHeader(code) 49 errMsg, _ := json.Marshal(struct { 50 Error string 51 }{Error: msg}) 52 w.Write(errMsg) 53 } 54 55 func (h GraphiQL) ServeHTTP(w http.ResponseWriter, r *http.Request) { 56 if r.Method != http.MethodGet { 57 respErr(w, "only GET allowed", http.StatusMethodNotAllowed) 58 return 59 } 60 switch r.URL.Path { 61 case "/graphql/ui/graphiql.min.css": 62 data, err := graphiql.Assets.ReadFile(filepath.Base(r.URL.Path)) 63 if err != nil { 64 log.Warn("Error loading graphiql asset", "err", err) 65 respErr(w, "internal error", http.StatusInternalServerError) 66 return 67 } 68 respOk(w, data, "text/css") 69 case "/graphql/ui/graphiql.min.js", 70 "/graphql/ui/react.production.min.js", 71 "/graphql/ui/react-dom.production.min.js": 72 data, err := graphiql.Assets.ReadFile(filepath.Base(r.URL.Path)) 73 if err != nil { 74 log.Warn("Error loading graphiql asset", "err", err) 75 respErr(w, "internal error", http.StatusInternalServerError) 76 return 77 } 78 respOk(w, data, "application/javascript; charset=utf-8") 79 default: 80 data, err := graphiql.Assets.ReadFile("index.html") 81 if err != nil { 82 log.Warn("Error loading graphiql asset", "err", err) 83 respErr(w, "internal error", http.StatusInternalServerError) 84 return 85 } 86 respOk(w, data, "text/html") 87 } 88 }