use init function to inline template

This commit is contained in:
Pierre Dubouilh 2024-08-04 21:08:13 +02:00 committed by Pierre Dubouilh
parent 1861de0d57
commit 5fbc140e53
2 changed files with 15 additions and 7 deletions

View file

@ -137,9 +137,9 @@ func replyList(w http.ResponseWriter, r *http.Request, fullPath string, path str
gz, err := gzip.NewWriterLevel(w, gzip.BestSpeed) // BestSpeed is Much Faster than default - base on a very unscientific local test, and only ~30% larger (compression remains still very effective, ~6x)
check(err)
defer gz.Close()
templateParsed.Execute(gz, p)
tmpl.Execute(gz, p)
} else {
templateParsed.Execute(w, p)
tmpl.Execute(w, p)
}
}

View file

@ -17,10 +17,18 @@ var styleCss string
var faviconSvg []byte
//go:embed ui/ui.tmpl
var template0 string
var uiTmpl string
var tmpl *template.Template
// fill in template
var template1 = strings.Replace(template0, "css_will_be_here", styleCss, 1)
var template2 = strings.Replace(template1, "js_will_be_here", scriptJs, 1)
var template3 = strings.Replace(template2, "favicon_will_be_here", base64.StdEncoding.EncodeToString(faviconSvg), 2)
var templateParsed, _ = template.New("").Parse(template3)
func init() {
var err error
t := strings.Replace(uiTmpl, "css_will_be_here", styleCss, 1)
t = strings.Replace(t, "js_will_be_here", scriptJs, 1)
t = strings.Replace(t, "favicon_will_be_here", base64.StdEncoding.EncodeToString(faviconSvg), 2)
tmpl, err = template.New("").Parse(t)
if err != nil {
panic(err)
}
}