summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2022-05-16 18:55:08 +0200
committerxengineering <me@xengineering.eu>2022-05-16 18:55:08 +0200
commit96e66b41169a005613fe45f47bb7d1d92950a10f (patch)
treeecfe5679b4b7c67b77d9367f37e6e6fe94c6cbd2
parent0344477b6014ca879d11934ef2d6fbd8a4c290c3 (diff)
downloadwebiot-96e66b41169a005613fe45f47bb7d1d92950a10f.tar
webiot-96e66b41169a005613fe45f47bb7d1d92950a10f.tar.zst
webiot-96e66b41169a005613fe45f47bb7d1d92950a10f.zip
Introduce appdata configuration option
-rw-r--r--config.json6
-rw-r--r--main.go15
2 files changed, 11 insertions, 10 deletions
diff --git a/config.json b/config.json
index b939d6d..513e364 100644
--- a/config.json
+++ b/config.json
@@ -6,7 +6,7 @@
]
},
"web":{
- "listen":"127.0.0.1:9000",
- "templates":"/usr/share/webiot/"
- }
+ "listen":"127.0.0.1:9000"
+ },
+ "appdata":"/usr/share/webiot/"
}
diff --git a/main.go b/main.go
index b8fe2e2..76e9f85 100644
--- a/main.go
+++ b/main.go
@@ -18,6 +18,7 @@ import (
type RuntimeConfig struct {
Devices DevicesConfig
Web WebConfig
+ Appdata string
}
type DevicesConfig struct {
@@ -26,16 +27,15 @@ type DevicesConfig struct {
type WebConfig struct {
Listen netip.AddrPort
- Templates string
}
// main() contains the control flow of this program.
func main() {
configPath := parseFlags()
c := parseConfig(configPath)
- http.HandleFunc("/", index(c.Devices, c.Web.Templates))
+ http.HandleFunc("/", index(c.Devices, c.Appdata))
http.HandleFunc("/api", api())
- http.HandleFunc("/webiot.css", css())
+ http.HandleFunc("/webiot.css", css(c.Appdata))
fmt.Printf("Serving at http://%s\n", c.Web.Listen)
log.Fatal(http.ListenAndServe(c.Web.Listen.String(), nil))
}
@@ -73,10 +73,10 @@ func parseConfig(path string) RuntimeConfig {
}
// index() returns a HTTP handler for the index page.
-func index(devices DevicesConfig, templates string) func(http.ResponseWriter, *http.Request) {
+func index(devices DevicesConfig, appdata string) func(http.ResponseWriter, *http.Request) {
// prepare HTML
- path := filepath.Join(templates, "index.html.tmpl")
+ path := filepath.Join(appdata, "index.html.tmpl")
html := mustRender(path, devices)
return func(w http.ResponseWriter, r *http.Request) {
@@ -84,10 +84,11 @@ func index(devices DevicesConfig, templates string) func(http.ResponseWriter, *h
}
}
-func css() func(http.ResponseWriter, *http.Request) {
+func css(appdata string) func(http.ResponseWriter, *http.Request) {
// read CSS file
- css := string(mustRead("./libweb/libweb.css"))
+ path := filepath.Join(appdata, "libweb/libweb.css")
+ css := string(mustRead(path))
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/css; charset=utf-8")