serve bundled index.html

This commit is contained in:
Nazar Kanaev 2020-08-20 11:59:57 +01:00
parent 06458065d6
commit 58946bac36
2 changed files with 32 additions and 14 deletions

View File

@ -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)
}

View File

@ -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) {