mirror of
https://github.com/nkanaev/yarr.git
synced 2025-05-24 00:33:14 +00:00
serve bundled index.html
This commit is contained in:
parent
06458065d6
commit
58946bac36
19
bundle.go
19
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)
|
||||
}
|
||||
|
@ -89,11 +89,15 @@ type ItemUpdateForm struct {
|
||||
}
|
||||
|
||||
func IndexHandler(rw http.ResponseWriter, req *http.Request) {
|
||||
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 {
|
||||
if asset, ok := assets["graphicarts/" + svg]; ok {
|
||||
return template.HTML(*asset.text())
|
||||
}
|
||||
content, _ := ioutil.ReadFile("assets/graphicarts/" + svg)
|
||||
return template.HTML(content)
|
||||
},
|
||||
@ -101,6 +105,7 @@ func IndexHandler(rw http.ResponseWriter, req *http.Request) {
|
||||
rw.Header().Set("Content-Type", "text/html")
|
||||
t.Execute(rw, nil)
|
||||
}
|
||||
}
|
||||
|
||||
func StaticHandler(rw http.ResponseWriter, req *http.Request) {
|
||||
path := Vars(req)["path"]
|
||||
|
Loading…
x
Reference in New Issue
Block a user