diff options
author | xengineering <me@xengineering.eu> | 2022-05-16 18:46:37 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2022-05-16 18:46:37 +0200 |
commit | 0344477b6014ca879d11934ef2d6fbd8a4c290c3 (patch) | |
tree | 2b72481bbba479000c409504dcb24452324f3aa2 | |
parent | fa5433ddc8c3bb8d0ba184fe91df79365fd98a58 (diff) | |
download | webiot-0344477b6014ca879d11934ef2d6fbd8a4c290c3.tar webiot-0344477b6014ca879d11934ef2d6fbd8a4c290c3.tar.zst webiot-0344477b6014ca879d11934ef2d6fbd8a4c290c3.zip |
Add configuration of template path
-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) |