From 96e66b41169a005613fe45f47bb7d1d92950a10f Mon Sep 17 00:00:00 2001 From: xengineering Date: Mon, 16 May 2022 18:55:08 +0200 Subject: Introduce appdata configuration option --- config.json | 6 +++--- main.go | 15 ++++++++------- 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") -- cgit v1.2.3-70-g09d2