Skip to content

Commit a9b7481

Browse files
willnorrisalexelisenko
authored andcommitted
client/web: combine embeds into a single embed.FS
instead of embedding each file individually, embed them all into a single embed filesystem. This is basically a noop for the current frontend, but sets things up a little cleaner for the new frontend. Also added an embed.FS for the source files needed to build the new frontend. These files are not actually embedded into the binary (since it is a blank identifier), but causes `go mod vendor` to copy them into the vendor directory. Updates tailscale/corp#13775 Signed-off-by: Will Norris <[email protected]> Signed-off-by: Alex Paguis <[email protected]>
1 parent f171464 commit a9b7481

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

client/web/web.go

+17-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"context"
1010
"crypto/rand"
1111
"crypto/tls"
12-
_ "embed"
12+
"embed"
1313
"encoding/json"
1414
"encoding/xml"
1515
"fmt"
@@ -36,16 +36,19 @@ import (
3636
"tailscale.com/version/distro"
3737
)
3838

39-
//go:embed web.html
40-
var webHTML string
41-
42-
//go:embed web.css
43-
var webCSS string
39+
// This contains all files needed to build the frontend assets.
40+
// Because we assign this to the blank identifier, it does not actually embed the files.
41+
// However, this does cause `go mod vendor` to include the files when vendoring the package.
42+
// External packages that use the web client can `go mod vendor`, run `yarn build` to
43+
// build the assets, then those asset bundles will be able to be embedded.
44+
//
45+
//go:embed yarn.lock index.html *.js *.json src/**/*
46+
var _ embed.FS
4447

45-
//go:embed auth-redirect.html
46-
var authenticationRedirectHTML string
48+
//go:embed web.html web.css auth-redirect.html
49+
var embeddedFS embed.FS
4750

48-
var tmpl *template.Template
51+
var tmpls *template.Template
4952

5053
// Server is the backend server for a Tailscale web client.
5154
type Server struct {
@@ -84,8 +87,7 @@ func NewServer(devMode bool, lc *tailscale.LocalClient) (s *Server, cleanup func
8487
}
8588

8689
func init() {
87-
tmpl = template.Must(template.New("web.html").Parse(webHTML))
88-
template.Must(tmpl.New("web.css").Parse(webCSS))
90+
tmpls = template.Must(template.New("").ParseFS(embeddedFS, "*"))
8991
}
9092

9193
// authorize returns the name of the user accessing the web UI after verifying
@@ -301,7 +303,9 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
301303

302304
switch {
303305
case r.URL.Path == "/redirect" || r.URL.Path == "/redirect/":
304-
io.WriteString(w, authenticationRedirectHTML)
306+
if err := tmpls.ExecuteTemplate(w, "auth-redirect.html", nil); err != nil {
307+
http.Error(w, err.Error(), http.StatusInternalServerError)
308+
}
305309
return
306310
case r.Method == "POST":
307311
s.servePostNodeUpdate(w, r)
@@ -380,7 +384,7 @@ func (s *Server) serveGetNodeData(w http.ResponseWriter, r *http.Request, user s
380384
return
381385
}
382386
buf := new(bytes.Buffer)
383-
if err := tmpl.Execute(buf, *data); err != nil {
387+
if err := tmpls.ExecuteTemplate(buf, "web.html", data); err != nil {
384388
http.Error(w, err.Error(), http.StatusInternalServerError)
385389
return
386390
}

0 commit comments

Comments
 (0)