From 7dd778cda189ea7debc7770a030fc831b9eab18b Mon Sep 17 00:00:00 2001 From: xengineering Date: Mon, 25 Mar 2024 20:14:37 +0100 Subject: Move template parsing to main function --- main.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 80247e8..6d930b4 100644 --- a/main.go +++ b/main.go @@ -16,11 +16,20 @@ var templates *template.Template func main() { configPath := parseFlags() c := parseConfig(configPath) + + var err error + templates, err = template.ParseFS(static, "templates/index.html") + if err != nil { + log.Fatal(err) + } + http.HandleFunc("/", index(c.Devices)) http.HandleFunc("/api", api) http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(static)))) + fmt.Printf("Serving at http://%s\n", c.Web.Listen) + log.Fatal(http.ListenAndServe(c.Web.Listen.String(), nil)) } @@ -38,13 +47,8 @@ func parseFlags() string { // index() returns a HTTP handler for the index page. func index(devices DevicesConfig) func(http.ResponseWriter, *http.Request) { - templates, err := template.ParseFS(static, "templates/index.html") - if err != nil { - log.Fatal(err) - } - return func(w http.ResponseWriter, r *http.Request) { - err = templates.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