Skip to content

Commit 889a902

Browse files
prattmicgopherbot
authored andcommitted
cmd/trace: embed static content
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 <[email protected]> Auto-Submit: Michael Pratt <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 430ae97 commit 889a902

File tree

5 files changed

+12
-20
lines changed

5 files changed

+12
-20
lines changed

src/bootstrap.bash

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ if [ "$BOOTSTRAP_FORMAT" = "mintgz" ]; then
9696
echo "Preparing to generate build system's ${OUTGZ}; cleaning ..."
9797
rm -rf bin/gofmt
9898
rm -rf src/runtime/race/race_*.syso
99-
rm -rf api test doc misc/cgo/test misc/trace
99+
rm -rf api test doc misc/cgo/test
100100
rm -rf pkg/tool/*_*/{addr2line,api,cgo,cover,doc,fix,nm,objdump,pack,pprof,test2json,trace,vet}
101101
rm -rf pkg/*_*/{image,database,cmd}
102102
rm -rf $(find . -type d -name testdata)

misc/trace/README.md renamed to src/cmd/trace/static/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ The file was generated by catapult's `vulcanize_trace_viewer` command.
1717
$ git clone https://chromium.googlesource.com/catapult
1818
$ cd catapult
1919
$ ./tracing/bin/vulcanize_trace_viewer --config=full
20-
$ cp tracing/bin/trace_viewer_full.html $GOROOT/misc/trace/trace_viewer_full.html
20+
$ cp tracing/bin/trace_viewer_full.html $GOROOT/src/cmd/trace/static/trace_viewer_full.html
2121
```
2222

2323
We are supposed to use --config=lean (produces smaller html),
@@ -31,7 +31,7 @@ to import the `trace_viewer_full.html`.
3131
This is copied from the catapult repo.
3232

3333
```
34-
$ cp third_party/polymer/components/webcomponentsjs/webcomponents.min.js $GOROOT/misc/trace/webcomponents.min.js
34+
$ cp third_party/polymer/components/webcomponentsjs/webcomponents.min.js $GOROOT/src/cmd/trace/static/webcomponents.min.js
3535
```
3636

3737
## Licenses

src/cmd/trace/trace.go

+9-17
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,31 @@ package main
66

77
import (
88
"cmd/internal/traceviewer"
9+
"embed"
910
"encoding/json"
1011
"fmt"
1112
"internal/trace"
1213
"io"
1314
"log"
1415
"math"
1516
"net/http"
16-
"path/filepath"
17-
"runtime"
1817
"runtime/debug"
1918
"sort"
2019
"strconv"
2120
"strings"
2221
"time"
2322
)
2423

24+
//go:embed static/trace_viewer_full.html static/webcomponents.min.js
25+
var staticContent embed.FS
26+
2527
func init() {
2628
http.HandleFunc("/trace", httpTrace)
2729
http.HandleFunc("/jsontrace", httpJsonTrace)
28-
http.HandleFunc("/trace_viewer_html", httpTraceViewerHTML)
29-
http.HandleFunc("/webcomponents.min.js", webcomponentsJS)
30+
http.Handle("/static/", http.FileServer(http.FS(staticContent)))
3031
}
3132

33+
3234
// httpTrace serves either whole trace (goid==0) or trace for goid goroutine.
3335
func httpTrace(w http.ResponseWriter, r *http.Request) {
3436
_, err := parseTrace()
@@ -50,19 +52,19 @@ func httpTrace(w http.ResponseWriter, r *http.Request) {
5052
var templTrace = `
5153
<html>
5254
<head>
53-
<script src="/webcomponents.min.js"></script>
55+
<script src="/static/webcomponents.min.js"></script>
5456
<script>
5557
'use strict';
5658
5759
function onTraceViewerImportFail() {
5860
document.addEventListener('DOMContentLoaded', function() {
5961
document.body.textContent =
60-
'/trace_viewer_full.html is missing. File a bug in https://golang.org/issue';
62+
'/static/trace_viewer_full.html is missing. File a bug in https://golang.org/issue';
6163
});
6264
}
6365
</script>
6466
65-
<link rel="import" href="https://github.com/trace_viewer_html"
67+
<link rel="import" href="https://github.com/static/trace_viewer_full.html"
6668
onerror="onTraceViewerImportFail(event)">
6769
6870
<style type="text/css">
@@ -173,16 +175,6 @@ function onTraceViewerImportFail() {
173175
</html>
174176
`
175177

176-
// httpTraceViewerHTML serves static part of trace-viewer.
177-
// This URL is queried from templTrace HTML.
178-
func httpTraceViewerHTML(w http.ResponseWriter, r *http.Request) {
179-
http.ServeFile(w, r, filepath.Join(runtime.GOROOT(), "misc", "trace", "trace_viewer_full.html"))
180-
}
181-
182-
func webcomponentsJS(w http.ResponseWriter, r *http.Request) {
183-
http.ServeFile(w, r, filepath.Join(runtime.GOROOT(), "misc", "trace", "webcomponents.min.js"))
184-
}
185-
186178
// httpJsonTrace serves json trace, requested from within templTrace HTML.
187179
func httpJsonTrace(w http.ResponseWriter, r *http.Request) {
188180
defer debug.FreeOSMemory()

0 commit comments

Comments
 (0)