Skip to content

Commit f740ced

Browse files
committed
cmd/relui: use go:embed for templates and content
This changes relui to use embed from go1.16 for managing content. This drastically simplifies the deployment steps, in preparation for containerizing. For golang/go#47401 Change-Id: I1eb9f6f63fa490ef73ed454ddecd1fd99db4f960 Reviewed-on: https://go-review.googlesource.com/c/build/+/340429 Trust: Alexander Rakoczy <[email protected]> Run-TryBot: Alexander Rakoczy <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]>
1 parent 2cf5c44 commit f740ced

File tree

6 files changed

+53
-19
lines changed

6 files changed

+53
-19
lines changed

cmd/relui/content.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2021 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+
5+
//go:build go1.16
6+
// +build go1.16
7+
8+
package main
9+
10+
import "embed"
11+
12+
// static is our static web server content.
13+
//go:embed static
14+
var static embed.FS
15+
16+
// templates are our template files.
17+
//go:embed templates
18+
var templates embed.FS

cmd/relui/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
//go:build go1.16
6+
// +build go1.16
7+
58
// relui is a web interface for managing the release process of Go.
69
package main
710

@@ -44,7 +47,7 @@ func main() {
4447
http.Handle("/workflows/create", http.HandlerFunc(s.createWorkflowHandler))
4548
http.Handle("/workflows/new", http.HandlerFunc(s.newWorkflowHandler))
4649
http.Handle("/tasks/start", http.HandlerFunc(s.startTaskHandler))
47-
http.Handle("/", fileServerHandler(relativeFile("./static"), http.HandlerFunc(s.homeHandler)))
50+
http.Handle("/", fileServerHandler(static, http.HandlerFunc(s.homeHandler)))
4851
port := os.Getenv("PORT")
4952
if port == "" {
5053
port = "8080"

cmd/relui/store.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
//go:build go1.16
6+
// +build go1.16
7+
58
package main
69

710
import (

cmd/relui/templates/layout.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<html lang="en">
88
<title>Go Releases</title>
99
<meta name="viewport" content="width=device-width, initial-scale=1" />
10-
<link rel="stylesheet" href="/styles.css" />
10+
<link rel="stylesheet" href="/static/styles.css" />
1111
<body class="Site">
1212
<header class="Site-header">
1313
<div class="Header">

cmd/relui/web.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
//go:build go1.16
6+
// +build go1.16
7+
58
package main
69

710
import (
811
"bytes"
912
"html/template"
1013
"io"
14+
"io/fs"
1115
"log"
1216
"mime"
1317
"net/http"
@@ -21,33 +25,28 @@ import (
2125
reluipb "golang.org/x/build/cmd/relui/protos"
2226
)
2327

24-
// fileServerHandler returns a http.Handler rooted at root. It will call the next handler provided for requests to "/".
28+
// fileServerHandler returns a http.Handler rooted at root. It will
29+
// call the next handler provided for requests to "/".
2530
//
26-
// The returned handler sets the appropriate Content-Type and Cache-Control headers for the returned file.
27-
func fileServerHandler(root string, next http.Handler) http.Handler {
31+
// The returned handler sets the appropriate Content-Type and
32+
// Cache-Control headers for the returned file.
33+
func fileServerHandler(fs fs.FS, next http.Handler) http.Handler {
2834
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2935
if r.URL.Path == "/" {
3036
next.ServeHTTP(w, r)
3137
return
3238
}
33-
// http.FileServer would correctly return a 404, but we need to check that the file exists
34-
// before calculating the Content-Type header.
35-
if _, err := os.Stat(path.Join(root, r.URL.Path)); os.IsNotExist(err) {
36-
http.NotFound(w, r)
37-
return
38-
}
3939
w.Header().Set("Content-Type", mime.TypeByExtension(path.Ext(r.URL.Path)))
4040
w.Header().Set("Cache-Control", "no-cache, private, max-age=0")
41-
42-
fs := http.FileServer(http.Dir(root))
43-
fs.ServeHTTP(w, r)
41+
s := http.FileServer(http.FS(fs))
42+
s.ServeHTTP(w, r)
4443
})
4544
}
4645

4746
var (
48-
homeTmpl = template.Must(template.Must(layoutTmpl.Clone()).ParseFiles(relativeFile("templates/home.html")))
49-
layoutTmpl = template.Must(template.ParseFiles(relativeFile("templates/layout.html")))
50-
newWorkflowTmpl = template.Must(template.Must(layoutTmpl.Clone()).ParseFiles(relativeFile("templates/new_workflow.html")))
47+
homeTmpl = template.Must(template.Must(layoutTmpl.Clone()).ParseFS(templates, "templates/home.html"))
48+
layoutTmpl = template.Must(template.ParseFS(templates, "templates/layout.html"))
49+
newWorkflowTmpl = template.Must(template.Must(layoutTmpl.Clone()).ParseFS(templates, "templates/new_workflow.html"))
5150
)
5251

5352
// server implements the http handlers for relui.

cmd/relui/web_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
//go:build go1.16
6+
// +build go1.16
7+
58
package main
69

710
import (
811
"context"
12+
"embed"
913
"io/ioutil"
1014
"net/http"
1115
"net/http/httptest"
@@ -21,8 +25,12 @@ import (
2125
"google.golang.org/grpc"
2226
)
2327

28+
// testStatic is our static web server content.
29+
//go:embed testing
30+
var testStatic embed.FS
31+
2432
func TestFileServerHandler(t *testing.T) {
25-
h := fileServerHandler("./testing", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
33+
h := fileServerHandler(testStatic, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2634
w.Write([]byte("Home"))
2735
}))
2836

@@ -41,7 +49,7 @@ func TestFileServerHandler(t *testing.T) {
4149
},
4250
{
4351
desc: "sets headers and returns file",
44-
path: "/test.css",
52+
path: "/testing/test.css",
4553
wantCode: http.StatusOK,
4654
wantBody: ".Header { font-size: 10rem; }\n",
4755
wantHeaders: map[string]string{
@@ -54,6 +62,9 @@ func TestFileServerHandler(t *testing.T) {
5462
path: "/foo.js",
5563
wantCode: http.StatusNotFound,
5664
wantBody: "404 page not found\n",
65+
wantHeaders: map[string]string{
66+
"Content-Type": "text/plain; charset=utf-8",
67+
},
5768
},
5869
}
5970
for _, c := range cases {

0 commit comments

Comments
 (0)