summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2024-03-25 20:14:37 +0100
committerxengineering <me@xengineering.eu>2024-03-25 20:14:37 +0100
commit7dd778cda189ea7debc7770a030fc831b9eab18b (patch)
tree842aae9140a8a5adbb21a7f5e429328f372a1c76
parent560999995ff4d8e9ec3393c3aaca4d7fbace7c95 (diff)
downloadwebiot-7dd778cda189ea7debc7770a030fc831b9eab18b.tar
webiot-7dd778cda189ea7debc7770a030fc831b9eab18b.tar.zst
webiot-7dd778cda189ea7debc7770a030fc831b9eab18b.zip
Move template parsing to main function
-rw-r--r--main.go16
1 files 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