diff options
author | xengineering <me@xengineering.eu> | 2024-03-25 20:14:37 +0100 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2024-03-25 20:14:37 +0100 |
commit | 7dd778cda189ea7debc7770a030fc831b9eab18b (patch) | |
tree | 842aae9140a8a5adbb21a7f5e429328f372a1c76 /main.go | |
parent | 560999995ff4d8e9ec3393c3aaca4d7fbace7c95 (diff) | |
download | webiot-7dd778cda189ea7debc7770a030fc831b9eab18b.tar webiot-7dd778cda189ea7debc7770a030fc831b9eab18b.tar.zst webiot-7dd778cda189ea7debc7770a030fc831b9eab18b.zip |
Move template parsing to main function
Diffstat (limited to 'main.go')
-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 |