Skip to content

Commit 51f101b

Browse files
committed
internal/content/telemetrygodev: add missing privacy.html
CL 599275 inadvertently broke the telemetry privacy policy rendering, as it removed the base.html used by privacy policy markdown rendering (see also Chesterton's Fence...). Fix this by adding back a root template for markdown rendering, which can no longer share a common base with other layouts that have since been modified to manage their own content layout. Also: - temporarily move necessary template funcs into the content package - add a test that would have caught this breakage Fixes golang/go#68859 Change-Id: I4afa76387b97fe5219bebb6c1147266775c16bea Reviewed-on: https://go-review.googlesource.com/c/telemetry/+/604798 TryBot-Bypass: Robert Findley <[email protected]> Reviewed-by: Hongxiang Jiang <[email protected]> Auto-Submit: Robert Findley <[email protected]> Commit-Queue: Robert Findley <[email protected]>
1 parent 38c23d2 commit 51f101b

File tree

6 files changed

+51
-23
lines changed

6 files changed

+51
-23
lines changed

godev/cmd/telemetrygodev/main.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"errors"
1313
"flag"
1414
"fmt"
15-
"html/template"
1615
"io"
1716
"io/fs"
1817
"log"
@@ -67,7 +66,7 @@ func newHandler(ctx context.Context, cfg *config.Config) http.Handler {
6766
mux := http.NewServeMux()
6867

6968
render := func(w http.ResponseWriter, tmpl string, page any) error {
70-
return content.Template(w, fsys, tmpl, chartFuncs(), page, http.StatusOK)
69+
return content.Template(w, fsys, tmpl, page, http.StatusOK)
7170
}
7271

7372
logger := slog.Default()
@@ -89,20 +88,6 @@ func newHandler(ctx context.Context, cfg *config.Config) http.Handler {
8988
return mw(mux)
9089
}
9190

92-
func chartFuncs() template.FuncMap {
93-
return template.FuncMap{
94-
"chartName": func(name string) string {
95-
name, _, _ = strings.Cut(name, ":")
96-
return name
97-
},
98-
"programName": func(name string) string {
99-
name = strings.TrimPrefix(name, "golang.org/")
100-
name = strings.TrimPrefix(name, "github.com/")
101-
return name
102-
},
103-
}
104-
}
105-
10691
// breadcrumb holds a breadcrumb nav element.
10792
//
10893
// If Link is empty, breadcrumbs are rendered as plain text.
@@ -401,6 +386,6 @@ func handleConfig(fsys fs.FS, ucfg *tconfig.Config) content.HandlerFunc {
401386
ChartConfig: string(ccfg),
402387
UploadConfig: string(cfgJSON),
403388
}
404-
return content.Template(w, fsys, "config.html", chartFuncs(), page, http.StatusOK)
389+
return content.Template(w, fsys, "config.html", page, http.StatusOK)
405390
}
406391
}

godev/cmd/telemetrygodev/main_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ func TestPaths(t *testing.T) {
105105
fragments []string
106106
}{
107107
{"GET", "/", "", 200, []string{"Go Telemetry"}},
108+
{"GET", "/privacy", "", 200, []string{"Privacy Policy"}},
109+
{"GET", "/config", "", 200, []string{"Chart Config"}},
108110
{
109111
"POST",
110112
"/upload/2023-01-01/123.json",

godev/internal/content/content.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (c *contentServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
138138
}
139139
switch path.Ext(filepath) {
140140
case ".html":
141-
err = Template(w, c.fsys, filepath, nil, nil, http.StatusOK)
141+
err = Template(w, c.fsys, filepath, nil, http.StatusOK)
142142
case ".md":
143143
err = markdown(w, c.fsys, filepath, http.StatusOK)
144144
default:
@@ -152,13 +152,13 @@ func (c *contentServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
152152

153153
// Template executes a template response.
154154
// TODO(rfindley): this abstraction no longer holds its weight. Refactor.
155-
func Template(w http.ResponseWriter, fsys fs.FS, tmplPath string, funcs template.FuncMap, data any, code int) error {
155+
func Template(w http.ResponseWriter, fsys fs.FS, tmplPath string, data any, code int) error {
156156
patterns, err := tmplPatterns(fsys, tmplPath)
157157
if err != nil {
158158
return err
159159
}
160160
patterns = append(patterns, tmplPath)
161-
tmpl, err := template.New("").Funcs(funcs).ParseFS(fsys, patterns...)
161+
tmpl, err := template.New("").Funcs(chartFuncs()).ParseFS(fsys, patterns...)
162162
if err != nil {
163163
return err
164164
}
@@ -178,6 +178,22 @@ func Template(w http.ResponseWriter, fsys fs.FS, tmplPath string, funcs template
178178
return nil
179179
}
180180

181+
// TODO(rfindley): refactor so that these funcs are only required by templates
182+
// that use them.
183+
func chartFuncs() template.FuncMap {
184+
return template.FuncMap{
185+
"chartName": func(name string) string {
186+
name, _, _ = strings.Cut(name, ":")
187+
return name
188+
},
189+
"programName": func(name string) string {
190+
name = strings.TrimPrefix(name, "golang.org/")
191+
name = strings.TrimPrefix(name, "github.com/")
192+
return name
193+
},
194+
}
195+
}
196+
181197
// JSON encodes data as JSON response with a status code.
182198
func JSON(w http.ResponseWriter, data any, code int) error {
183199
var buf bytes.Buffer
@@ -291,7 +307,7 @@ func markdown(w http.ResponseWriter, fsys fs.FS, tmplPath string, code int) erro
291307
if !ok {
292308
return errors.New("missing layout for template " + tmplPath)
293309
}
294-
return Template(w, fsys, layout.(string), nil, data, code)
310+
return Template(w, fsys, layout.(string), data, code)
295311
}
296312

297313
// stat trys to coerce a urlPath into an openable file then returns the

godev/internal/content/content_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func Test_stat(t *testing.T) {
174174

175175
func handleTemplate(fsys fs.FS) HandlerFunc {
176176
return func(w http.ResponseWriter, _ *http.Request) error {
177-
return Template(w, fsys, "data.html", nil, "Data from Handler", http.StatusOK)
177+
return Template(w, fsys, "data.html", "Data from Handler", http.StatusOK)
178178
}
179179
}
180180

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!--
2+
Copyright 2023 The Go Authors. All rights reserved.
3+
Use of this source code is governed by a BSD-style
4+
license that can be found in the LICENSE file.
5+
-->
6+
7+
<!DOCTYPE html>
8+
<html lang="en">
9+
10+
<head>
11+
<meta charset="UTF-8" />
12+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
13+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
14+
<title>{{block "title" .}}{{.Title}}{{end}}</title>
15+
<link rel="icon" type="image/x-icon" href="/static/favicon.ico">
16+
<link rel="stylesheet" href="/static/base.min.css">
17+
</head>
18+
<body>
19+
<div class="Container">
20+
<div class="Content">
21+
{{block "content" .}}{{.Content}}{{end}}
22+
</div>
23+
</div>
24+
</body>
25+
</html>

internal/content/telemetrygodev/privacy.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
Title: Go Telemetry Privacy Policy
3-
Layout: base.html
3+
Layout: privacy.html
44
---
55

66
# Privacy Policy

0 commit comments

Comments
 (0)