From 820b15f71a4ac4aeb0c31dc6b929f2d022cff3b8 Mon Sep 17 00:00:00 2001 From: xengineering Date: Sun, 24 Mar 2024 21:07:44 +0100 Subject: Replace appdata completely by embed package The embed package makes it useless to handle static files from the source tree during runtime. All those files go simply to the embed.FS variable and are thus embedded into the binary which is easier to handle. --- Makefile | 3 +-- appdata/index.html.tmpl | 53 ------------------------------------------------- main.go | 24 +++++++++++----------- templates/index.html | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 67 deletions(-) delete mode 100644 appdata/index.html.tmpl create mode 100644 templates/index.html diff --git a/Makefile b/Makefile index de7e4f3..c112fd9 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,6 @@ clean: install: all install -Dm 755 build/$(PROGRAM) $(DESTDIR)$(PREFIX)/bin/$(PROGRAM) install -Dm 644 config/default.json $(DESTDIR)/etc/$(PROGRAM)/config.json - install -Dm 644 appdata/index.html.tmpl $(DESTDIR)$(PREFIX)/share/$(PROGRAM)/index.html.tmpl debug: - go run *.go -c config/example.json -a appdata + go run *.go -c config/example.json diff --git a/appdata/index.html.tmpl b/appdata/index.html.tmpl deleted file mode 100644 index c9fa2f4..0000000 --- a/appdata/index.html.tmpl +++ /dev/null @@ -1,53 +0,0 @@ - - - - - - - webiot - - - - - - - - - -
-

webiot

-

A simple IoT web server

-
- -
{{range .Hs100}} -
-

{{.Name}}

- - -
{{end}} - - - - - -
- - - - diff --git a/main.go b/main.go index 5363831..adbdf34 100644 --- a/main.go +++ b/main.go @@ -10,15 +10,12 @@ import ( "log" "net/http" "net/netip" - "path/filepath" "text/template" ) -//go:embed simple.css/simple.css +//go:embed simple.css/simple.css templates/index.html var static embed.FS -var appdata string - type RuntimeConfig struct { Devices DevicesConfig Web WebConfig @@ -35,7 +32,7 @@ type WebConfig struct { func main() { configPath := parseFlags() c := parseConfig(configPath) - http.HandleFunc("/", index(c.Devices, appdata)) + http.HandleFunc("/", index(c.Devices)) http.HandleFunc("/api", api()) http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(static)))) @@ -50,8 +47,6 @@ func parseFlags() string { flag.StringVar(&r, "c", "/etc/webiot/config.json", "path to configuration file") - flag.StringVar(&appdata, "a", "/usr/share/webiot", - "path to static application data") flag.Parse() return r @@ -76,13 +71,18 @@ func parseConfig(path string) RuntimeConfig { } // index() returns a HTTP handler for the index page. -func index(devices DevicesConfig, appdata string) func(http.ResponseWriter, *http.Request) { - - path := filepath.Join(appdata, "index.html.tmpl") - html := mustRender(path, devices) +func index(devices DevicesConfig) func(http.ResponseWriter, *http.Request) { + tmpl, err := template.ParseFS(static, "templates/index.html") + if err != nil { + log.Fatal(err) + } return func(w http.ResponseWriter, r *http.Request) { - fmt.Fprint(w, html) + err = tmpl.Execute(w, devices) + if err != nil { + http.Error(w, fmt.Sprint(err), http.StatusInternalServerError) + return + } } } diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..c9fa2f4 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,53 @@ + + + + + + + webiot + + + + + + + + + +
+

webiot

+

A simple IoT web server

+
+ +
{{range .Hs100}} +
+

{{.Name}}

+ + +
{{end}} + + + + + +
+ + + + -- cgit v1.2.3-70-g09d2