From 560999995ff4d8e9ec3393c3aaca4d7fbace7c95 Mon Sep 17 00:00:00 2001 From: xengineering Date: Mon, 25 Mar 2024 20:10:12 +0100 Subject: Make parsed templates a global variable This allows to parse the templates during startup and use them in each HTTP handler without explicit passing to these handlers. --- main.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 511b434..80247e8 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( //go:embed simple.css/simple.css templates/index.html var static embed.FS +var templates *template.Template func main() { configPath := parseFlags() @@ -37,13 +38,13 @@ func parseFlags() string { // index() returns a HTTP handler for the index page. func index(devices DevicesConfig) func(http.ResponseWriter, *http.Request) { - tmpl, err := template.ParseFS(static, "templates/index.html") + templates, err := template.ParseFS(static, "templates/index.html") if err != nil { log.Fatal(err) } return func(w http.ResponseWriter, r *http.Request) { - err = tmpl.Execute(w, devices) + err = templates.Execute(w, devices) if err != nil { http.Error(w, fmt.Sprint(err), http.StatusInternalServerError) return -- cgit v1.2.3-70-g09d2