From 0344477b6014ca879d11934ef2d6fbd8a4c290c3 Mon Sep 17 00:00:00 2001 From: xengineering Date: Mon, 16 May 2022 18:46:37 +0200 Subject: Add configuration of template path --- config.json | 3 ++- 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/" } } diff --git a/main.go b/main.go index b70c193..b8fe2e2 100644 --- a/main.go +++ b/main.go @@ -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) -- cgit v1.2.3-70-g09d2