Skip to content

Commit

Permalink
internal/content/telemetrygodev: add missing privacy.html
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
findleyr committed Aug 13, 2024
1 parent 38c23d2 commit 51f101b
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 23 deletions.
19 changes: 2 additions & 17 deletions godev/cmd/telemetrygodev/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"errors"
"flag"
"fmt"
"html/template"
"io"
"io/fs"
"log"
Expand Down Expand Up @@ -67,7 +66,7 @@ func newHandler(ctx context.Context, cfg *config.Config) http.Handler {
mux := http.NewServeMux()

render := func(w http.ResponseWriter, tmpl string, page any) error {
return content.Template(w, fsys, tmpl, chartFuncs(), page, http.StatusOK)
return content.Template(w, fsys, tmpl, page, http.StatusOK)
}

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

func chartFuncs() template.FuncMap {
return template.FuncMap{
"chartName": func(name string) string {
name, _, _ = strings.Cut(name, ":")
return name
},
"programName": func(name string) string {
name = strings.TrimPrefix(name, "golang.org/")
name = strings.TrimPrefix(name, "github.com/")
return name
},
}
}

// breadcrumb holds a breadcrumb nav element.
//
// If Link is empty, breadcrumbs are rendered as plain text.
Expand Down Expand Up @@ -401,6 +386,6 @@ func handleConfig(fsys fs.FS, ucfg *tconfig.Config) content.HandlerFunc {
ChartConfig: string(ccfg),
UploadConfig: string(cfgJSON),
}
return content.Template(w, fsys, "config.html", chartFuncs(), page, http.StatusOK)
return content.Template(w, fsys, "config.html", page, http.StatusOK)
}
}
2 changes: 2 additions & 0 deletions godev/cmd/telemetrygodev/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ func TestPaths(t *testing.T) {
fragments []string
}{
{"GET", "/", "", 200, []string{"Go Telemetry"}},
{"GET", "/privacy", "", 200, []string{"Privacy Policy"}},
{"GET", "/config", "", 200, []string{"Chart Config"}},
{
"POST",
"/upload/2023-01-01/123.json",
Expand Down
24 changes: 20 additions & 4 deletions godev/internal/content/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (c *contentServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
switch path.Ext(filepath) {
case ".html":
err = Template(w, c.fsys, filepath, nil, nil, http.StatusOK)
err = Template(w, c.fsys, filepath, nil, http.StatusOK)
case ".md":
err = markdown(w, c.fsys, filepath, http.StatusOK)
default:
Expand All @@ -152,13 +152,13 @@ func (c *contentServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {

// Template executes a template response.
// TODO(rfindley): this abstraction no longer holds its weight. Refactor.
func Template(w http.ResponseWriter, fsys fs.FS, tmplPath string, funcs template.FuncMap, data any, code int) error {
func Template(w http.ResponseWriter, fsys fs.FS, tmplPath string, data any, code int) error {
patterns, err := tmplPatterns(fsys, tmplPath)
if err != nil {
return err
}
patterns = append(patterns, tmplPath)
tmpl, err := template.New("").Funcs(funcs).ParseFS(fsys, patterns...)
tmpl, err := template.New("").Funcs(chartFuncs()).ParseFS(fsys, patterns...)
if err != nil {
return err
}
Expand All @@ -178,6 +178,22 @@ func Template(w http.ResponseWriter, fsys fs.FS, tmplPath string, funcs template
return nil
}

// TODO(rfindley): refactor so that these funcs are only required by templates
// that use them.
func chartFuncs() template.FuncMap {
return template.FuncMap{
"chartName": func(name string) string {
name, _, _ = strings.Cut(name, ":")
return name
},
"programName": func(name string) string {
name = strings.TrimPrefix(name, "golang.org/")
name = strings.TrimPrefix(name, "github.com/")
return name
},
}
}

// JSON encodes data as JSON response with a status code.
func JSON(w http.ResponseWriter, data any, code int) error {
var buf bytes.Buffer
Expand Down Expand Up @@ -291,7 +307,7 @@ func markdown(w http.ResponseWriter, fsys fs.FS, tmplPath string, code int) erro
if !ok {
return errors.New("missing layout for template " + tmplPath)
}
return Template(w, fsys, layout.(string), nil, data, code)
return Template(w, fsys, layout.(string), data, code)
}

// stat trys to coerce a urlPath into an openable file then returns the
Expand Down
2 changes: 1 addition & 1 deletion godev/internal/content/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func Test_stat(t *testing.T) {

func handleTemplate(fsys fs.FS) HandlerFunc {
return func(w http.ResponseWriter, _ *http.Request) error {
return Template(w, fsys, "data.html", nil, "Data from Handler", http.StatusOK)
return Template(w, fsys, "data.html", "Data from Handler", http.StatusOK)
}
}

Expand Down
25 changes: 25 additions & 0 deletions internal/content/shared/privacy.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!--
Copyright 2023 The Go Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
-->

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>{{block "title" .}}{{.Title}}{{end}}</title>
<link rel="icon" type="image/x-icon" href="/static/favicon.ico">
<link rel="stylesheet" href="/static/base.min.css">
</head>
<body>
<div class="Container">
<div class="Content">
{{block "content" .}}{{.Content}}{{end}}
</div>
</div>
</body>
</html>
2 changes: 1 addition & 1 deletion internal/content/telemetrygodev/privacy.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
Title: Go Telemetry Privacy Policy
Layout: base.html
Layout: privacy.html
---

# Privacy Policy
Expand Down

0 comments on commit 51f101b

Please sign in to comment.