From 58946bac36850a91bf0e6482bae80e7ef47c64fc Mon Sep 17 00:00:00 2001 From: Nazar Kanaev Date: Thu, 20 Aug 2020 11:59:57 +0100 Subject: [PATCH] serve bundled index.html --- bundle.go | 19 ++++++++++++++++--- server/handlers.go | 27 ++++++++++++++++----------- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/bundle.go b/bundle.go index f29b828..f403880 100644 --- a/bundle.go +++ b/bundle.go @@ -10,6 +10,7 @@ import ( "path/filepath" "strings" "text/template" + htemplate "html/template" ) var code_template = `// autogenerated. do not edit! @@ -46,10 +47,8 @@ func encode(b []byte) string { } func main() { - outfile := "server/assets_bundle.go" assets := make([]asset, 0) filepatterns := []string{ - "assets/index.html", "assets/graphicarts/*.svg", "assets/graphicarts/*.png", "assets/javascripts/*.js", @@ -74,8 +73,22 @@ func main() { ) } } + var indexbuf bytes.Buffer + htemplate.Must(htemplate.New("index.html").Delims("{%", "%}").Funcs(htemplate.FuncMap{ + "inline": func(svg string) htemplate.HTML { + content, _ := ioutil.ReadFile("assets/graphicarts/" + svg) + return htemplate.HTML(content) + }, + }).ParseFiles("assets/index.html")).Execute(&indexbuf, nil) + indexcontent := indexbuf.Bytes() + assets = append(assets, asset{ + Name: "index.html", + Etag: shasum(indexcontent), + Body: encode(indexcontent), + }) + var buf bytes.Buffer template := template.Must(template.New("code").Parse(code_template)) template.Execute(&buf, assets) - ioutil.WriteFile(outfile, buf.Bytes(), 0644) + ioutil.WriteFile("server/assets_bundle.go", buf.Bytes(), 0644) } diff --git a/server/handlers.go b/server/handlers.go index b567d64..103d3ca 100644 --- a/server/handlers.go +++ b/server/handlers.go @@ -89,17 +89,22 @@ type ItemUpdateForm struct { } func IndexHandler(rw http.ResponseWriter, req *http.Request) { - t := template.Must(template.New("index.html").Delims("{%", "%}").Funcs(template.FuncMap{ - "inline": func(svg string) template.HTML { - if asset, ok := assets["graphicarts/" + svg]; ok { - return template.HTML(*asset.text()) - } - content, _ := ioutil.ReadFile("assets/graphicarts/" + svg) - return template.HTML(content) - }, - }).ParseFiles("assets/index.html")) - rw.Header().Set("Content-Type", "text/html") - t.Execute(rw, nil) + if assets != nil { + asset := assets["index.html"] + + rw.Header().Set("Content-Type", "text/html") + rw.Header().Set("Content-Encoding", "gzip") + rw.Write(*asset.gzip()) + } else { + t := template.Must(template.New("index.html").Delims("{%", "%}").Funcs(template.FuncMap{ + "inline": func(svg string) template.HTML { + content, _ := ioutil.ReadFile("assets/graphicarts/" + svg) + return template.HTML(content) + }, + }).ParseFiles("assets/index.html")) + rw.Header().Set("Content-Type", "text/html") + t.Execute(rw, nil) + } } func StaticHandler(rw http.ResponseWriter, req *http.Request) {