github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/tools/cmd/present/play.go (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "bytes" 9 "fmt" 10 "io/ioutil" 11 "net/http" 12 "path/filepath" 13 "time" 14 15 "golang.org/x/tools/godoc/static" 16 ) 17 18 var scripts = []string{"jquery.js", "jquery-ui.js", "playground.js", "play.js"} 19 20 // playScript registers an HTTP handler at /play.js that serves all the 21 // scripts specified by the variable above, and appends a line that 22 // initializes the playground with the specified transport. 23 func playScript(root, transport string) { 24 modTime := time.Now() 25 var buf bytes.Buffer 26 for _, p := range scripts { 27 if s, ok := static.Files[p]; ok { 28 buf.WriteString(s) 29 continue 30 } 31 b, err := ioutil.ReadFile(filepath.Join(root, "static", p)) 32 if err != nil { 33 panic(err) 34 } 35 buf.Write(b) 36 } 37 fmt.Fprintf(&buf, "\ninitPlayground(new %v());\n", transport) 38 b := buf.Bytes() 39 http.HandleFunc("/play.js", func(w http.ResponseWriter, r *http.Request) { 40 w.Header().Set("Content-type", "application/javascript") 41 http.ServeContent(w, r, "", modTime, bytes.NewReader(b)) 42 }) 43 }