From ce3a2dd68707c5c744aa019417baa12f1dab96e4 Mon Sep 17 00:00:00 2001 From: xengineering Date: Sun, 24 Mar 2024 12:14:30 +0100 Subject: Embed simple.css file into binary This removes the need to deploy the simple.css file to the target machine. --- Makefile | 1 - appdata/index.html.tmpl | 2 +- appdata/simple.css | 1 - main.go | 18 ++++++------------ 4 files changed, 7 insertions(+), 15 deletions(-) delete mode 120000 appdata/simple.css diff --git a/Makefile b/Makefile index 7ffa644..de7e4f3 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,6 @@ 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 - install -Dm 644 appdata/simple.css $(DESTDIR)$(PREFIX)/share/$(PROGRAM)/simple.css debug: go run *.go -c config/example.json -a appdata diff --git a/appdata/index.html.tmpl b/appdata/index.html.tmpl index 98f22ab..c9fa2f4 100644 --- a/appdata/index.html.tmpl +++ b/appdata/index.html.tmpl @@ -8,7 +8,7 @@ - + diff --git a/appdata/simple.css b/appdata/simple.css deleted file mode 120000 index 8f905d1..0000000 --- a/appdata/simple.css +++ /dev/null @@ -1 +0,0 @@ -../simple.css/simple.css \ No newline at end of file diff --git a/main.go b/main.go index a99b46b..5363831 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "bytes" "encoding/json" + "embed" "flag" "fmt" "io/ioutil" @@ -13,6 +14,9 @@ import ( "text/template" ) +//go:embed simple.css/simple.css +var static embed.FS + var appdata string type RuntimeConfig struct { @@ -33,7 +37,8 @@ func main() { c := parseConfig(configPath) http.HandleFunc("/", index(c.Devices, appdata)) http.HandleFunc("/api", api()) - http.HandleFunc("/simple.css", css(appdata)) + http.Handle("/static/", http.StripPrefix("/static/", + http.FileServer(http.FS(static)))) fmt.Printf("Serving at http://%s\n", c.Web.Listen) log.Fatal(http.ListenAndServe(c.Web.Listen.String(), nil)) } @@ -81,17 +86,6 @@ func index(devices DevicesConfig, appdata string) func(http.ResponseWriter, *htt } } -func css(appdata string) func(http.ResponseWriter, *http.Request) { - - path := filepath.Join(appdata, "simple.css") - css := string(mustRead(path)) - - return func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "text/css; charset=utf-8") - fmt.Fprint(w, css) - } -} - // mustRead() reads a file and panics if this is not possible. func mustRead(path string) []byte { data, err := ioutil.ReadFile(path) -- cgit v1.2.3-70-g09d2