diff options
author | xengineering <me@xengineering.eu> | 2024-03-25 20:10:12 +0100 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2024-03-25 20:10:12 +0100 |
commit | 560999995ff4d8e9ec3393c3aaca4d7fbace7c95 (patch) | |
tree | 29fe5acbea265144c37df61bc2ca0e88fb2c270b | |
parent | 82b6721e5fe7e626c6c731e408be9c989caf370c (diff) | |
download | webiot-560999995ff4d8e9ec3393c3aaca4d7fbace7c95.tar webiot-560999995ff4d8e9ec3393c3aaca4d7fbace7c95.tar.zst webiot-560999995ff4d8e9ec3393c3aaca4d7fbace7c95.zip |
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.
-rw-r--r-- | main.go | 5 |
1 files changed, 3 insertions, 2 deletions
@@ -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 |