]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/trace: embed static content
authorMichael Pratt <mpratt@google.com>
Wed, 12 Jan 2022 23:27:28 +0000 (18:27 -0500)
committerGopher Robot <gobot@golang.org>
Thu, 21 Apr 2022 21:18:18 +0000 (21:18 +0000)
cmd/trace is currently somewhat painful to use in odd environments since
it depends on the presence of $GOROOT/misc/trace to serve the static
trace viewer content.

Use //go:embed to embed this content directly into cmd/trace for easier
use.

Change-Id: I83b7d97dbecc9773f3b5a6b3bc4a6597473bc01a
Reviewed-on: https://go-review.googlesource.com/c/go/+/378194
Run-TryBot: Michael Pratt <mpratt@google.com>
Auto-Submit: Michael Pratt <mpratt@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/bootstrap.bash
src/cmd/trace/static/README.md [moved from misc/trace/README.md with 97% similarity]
src/cmd/trace/static/trace_viewer_full.html [moved from misc/trace/trace_viewer_full.html with 100% similarity]
src/cmd/trace/static/webcomponents.min.js [moved from misc/trace/webcomponents.min.js with 100% similarity]
src/cmd/trace/trace.go

index 88c080a9487e1a502bb1ba470e9a4e484194c79e..4038eaf9420c3784a8ecfdea1d305efa82a88a97 100755 (executable)
@@ -96,7 +96,7 @@ if [ "$BOOTSTRAP_FORMAT" = "mintgz" ]; then
        echo "Preparing to generate build system's ${OUTGZ}; cleaning ..."
        rm -rf bin/gofmt
        rm -rf src/runtime/race/race_*.syso
-       rm -rf api test doc misc/cgo/test misc/trace
+       rm -rf api test doc misc/cgo/test
        rm -rf pkg/tool/*_*/{addr2line,api,cgo,cover,doc,fix,nm,objdump,pack,pprof,test2json,trace,vet}
        rm -rf pkg/*_*/{image,database,cmd}
        rm -rf $(find . -type d -name testdata)
similarity index 97%
rename from misc/trace/README.md
rename to src/cmd/trace/static/README.md
index 218d7285461df9baf8506c325930783d3a6e86f9..f81c59eae5e463b070269663a4b333a756c21df4 100644 (file)
@@ -17,7 +17,7 @@ The file was generated by catapult's `vulcanize_trace_viewer` command.
 $ git clone https://chromium.googlesource.com/catapult
 $ cd catapult
 $ ./tracing/bin/vulcanize_trace_viewer --config=full
-$ cp tracing/bin/trace_viewer_full.html $GOROOT/misc/trace/trace_viewer_full.html
+$ cp tracing/bin/trace_viewer_full.html $GOROOT/src/cmd/trace/static/trace_viewer_full.html
 ```
 
 We are supposed to use --config=lean (produces smaller html),
@@ -31,7 +31,7 @@ to import the `trace_viewer_full.html`.
 This is copied from the catapult repo.
 
 ```
-$ cp third_party/polymer/components/webcomponentsjs/webcomponents.min.js $GOROOT/misc/trace/webcomponents.min.js
+$ cp third_party/polymer/components/webcomponentsjs/webcomponents.min.js $GOROOT/src/cmd/trace/static/webcomponents.min.js
 ```
 
 ## Licenses
index 0139639dae5c1e5f9d648d5818d00308467ab175..a0d742ac54f4f475d514697a21e680679dc7df07 100644 (file)
@@ -6,6 +6,7 @@ package main
 
 import (
        "cmd/internal/traceviewer"
+       "embed"
        "encoding/json"
        "fmt"
        "internal/trace"
@@ -13,8 +14,6 @@ import (
        "log"
        "math"
        "net/http"
-       "path/filepath"
-       "runtime"
        "runtime/debug"
        "sort"
        "strconv"
@@ -22,13 +21,16 @@ import (
        "time"
 )
 
+//go:embed static/trace_viewer_full.html static/webcomponents.min.js
+var staticContent embed.FS
+
 func init() {
        http.HandleFunc("/trace", httpTrace)
        http.HandleFunc("/jsontrace", httpJsonTrace)
-       http.HandleFunc("/trace_viewer_html", httpTraceViewerHTML)
-       http.HandleFunc("/webcomponents.min.js", webcomponentsJS)
+       http.Handle("/static/", http.FileServer(http.FS(staticContent)))
 }
 
+
 // httpTrace serves either whole trace (goid==0) or trace for goid goroutine.
 func httpTrace(w http.ResponseWriter, r *http.Request) {
        _, err := parseTrace()
@@ -50,19 +52,19 @@ func httpTrace(w http.ResponseWriter, r *http.Request) {
 var templTrace = `
 <html>
 <head>
-<script src="/webcomponents.min.js"></script>
+<script src="/static/webcomponents.min.js"></script>
 <script>
 'use strict';
 
 function onTraceViewerImportFail() {
   document.addEventListener('DOMContentLoaded', function() {
     document.body.textContent =
-    '/trace_viewer_full.html is missing. File a bug in https://golang.org/issue';
+    '/static/trace_viewer_full.html is missing. File a bug in https://golang.org/issue';
   });
 }
 </script>
 
-<link rel="import" href="/trace_viewer_html"
+<link rel="import" href="/static/trace_viewer_full.html"
       onerror="onTraceViewerImportFail(event)">
 
 <style type="text/css">
@@ -173,16 +175,6 @@ function onTraceViewerImportFail() {
 </html>
 `
 
-// httpTraceViewerHTML serves static part of trace-viewer.
-// This URL is queried from templTrace HTML.
-func httpTraceViewerHTML(w http.ResponseWriter, r *http.Request) {
-       http.ServeFile(w, r, filepath.Join(runtime.GOROOT(), "misc", "trace", "trace_viewer_full.html"))
-}
-
-func webcomponentsJS(w http.ResponseWriter, r *http.Request) {
-       http.ServeFile(w, r, filepath.Join(runtime.GOROOT(), "misc", "trace", "webcomponents.min.js"))
-}
-
 // httpJsonTrace serves json trace, requested from within templTrace HTML.
 func httpJsonTrace(w http.ResponseWriter, r *http.Request) {
        defer debug.FreeOSMemory()