diff options
-rw-r--r-- | config.json | 3 | ||||
-rw-r--r-- | main.go | 17 |
2 files changed, 12 insertions, 8 deletions
diff --git a/config.json b/config.json index 9366364..b939d6d 100644 --- a/config.json +++ b/config.json @@ -6,6 +6,7 @@ ] }, "web":{ - "listen":"127.0.0.1:9000" + "listen":"127.0.0.1:9000", + "templates":"/usr/share/webiot/" } } @@ -11,6 +11,7 @@ import ( "log" "net/http" "net/netip" + "path/filepath" "text/template" ) @@ -24,18 +25,19 @@ type DevicesConfig struct { } type WebConfig struct { - Listen netip.AddrPort + Listen netip.AddrPort + Templates string } // main() contains the control flow of this program. func main() { configPath := parseFlags() - config := parseConfig(configPath) - http.HandleFunc("/", index(config.Devices)) + c := parseConfig(configPath) + http.HandleFunc("/", index(c.Devices, c.Web.Templates)) http.HandleFunc("/api", api()) http.HandleFunc("/webiot.css", css()) - fmt.Printf("Serving at http://%s\n", config.Web.Listen) - log.Fatal(http.ListenAndServe(config.Web.Listen.String(), nil)) + fmt.Printf("Serving at http://%s\n", c.Web.Listen) + log.Fatal(http.ListenAndServe(c.Web.Listen.String(), nil)) } // parseFlags() handles command line interface (CLI) flags. @@ -71,10 +73,11 @@ func parseConfig(path string) RuntimeConfig { } // index() returns a HTTP handler for the index page. -func index(config DevicesConfig) func(http.ResponseWriter, *http.Request) { +func index(devices DevicesConfig, templates string) func(http.ResponseWriter, *http.Request) { // prepare HTML - html := mustRender("index.html.tmpl", config) + path := filepath.Join(templates, "index.html.tmpl") + html := mustRender(path, devices) return func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, html) |