diff options
author | xengineering <me@xengineering.eu> | 2024-03-24 12:14:30 +0100 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2024-03-24 12:15:40 +0100 |
commit | ce3a2dd68707c5c744aa019417baa12f1dab96e4 (patch) | |
tree | 7044a81de63c876ac9673874193a11576c14b7c8 | |
parent | a871e4f72aab1aa968adc72c4bf2e3e0c530550e (diff) | |
download | webiot-ce3a2dd68707c5c744aa019417baa12f1dab96e4.tar webiot-ce3a2dd68707c5c744aa019417baa12f1dab96e4.tar.zst webiot-ce3a2dd68707c5c744aa019417baa12f1dab96e4.zip |
Embed simple.css file into binary
This removes the need to deploy the simple.css file to the target
machine.
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | appdata/index.html.tmpl | 2 | ||||
l--------- | appdata/simple.css | 1 | ||||
-rw-r--r-- | main.go | 18 |
4 files changed, 7 insertions, 15 deletions
@@ -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 @@ <meta charset="utf-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"> - <link rel="stylesheet" href="./simple.css" type="text/css"> + <link rel="stylesheet" href="/static/simple.css/simple.css" type="text/css"> </head> 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 @@ -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) |