diff options
-rw-r--r-- | main.go | 16 |
1 files changed, 10 insertions, 6 deletions
@@ -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 |