summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2022-05-16 18:46:37 +0200
committerxengineering <me@xengineering.eu>2022-05-16 18:46:37 +0200
commit0344477b6014ca879d11934ef2d6fbd8a4c290c3 (patch)
tree2b72481bbba479000c409504dcb24452324f3aa2
parentfa5433ddc8c3bb8d0ba184fe91df79365fd98a58 (diff)
downloadwebiot-0344477b6014ca879d11934ef2d6fbd8a4c290c3.tar
webiot-0344477b6014ca879d11934ef2d6fbd8a4c290c3.tar.zst
webiot-0344477b6014ca879d11934ef2d6fbd8a4c290c3.zip
Add configuration of template path
-rw-r--r--config.json3
-rw-r--r--main.go17
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)