You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.1 KiB

  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. package main
  5. import (
  6. "bytes"
  7. "fmt"
  8. "io/ioutil"
  9. "net/http"
  10. "path/filepath"
  11. "time"
  12. "golang.org/x/tools/godoc/static"
  13. )
  14. var scripts = []string{"jquery.js", "jquery-ui.js", "playground.js", "play.js"}
  15. // playScript registers an HTTP handler at /play.js that serves all the
  16. // scripts specified by the variable above, and appends a line that
  17. // initializes the playground with the specified transport.
  18. func playScript(root, transport string) {
  19. modTime := time.Now()
  20. var buf bytes.Buffer
  21. for _, p := range scripts {
  22. if s, ok := static.Files[p]; ok {
  23. buf.WriteString(s)
  24. continue
  25. }
  26. b, err := ioutil.ReadFile(filepath.Join(root, "static", p))
  27. if err != nil {
  28. panic(err)
  29. }
  30. buf.Write(b)
  31. }
  32. fmt.Fprintf(&buf, "\ninitPlayground(new %v());\n", transport)
  33. b := buf.Bytes()
  34. http.HandleFunc("/play.js", func(w http.ResponseWriter, r *http.Request) {
  35. w.Header().Set("Content-type", "application/javascript")
  36. http.ServeContent(w, r, "", modTime, bytes.NewReader(b))
  37. })
  38. }