summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2024-03-24 12:14:30 +0100
committerxengineering <me@xengineering.eu>2024-03-24 12:15:40 +0100
commitce3a2dd68707c5c744aa019417baa12f1dab96e4 (patch)
tree7044a81de63c876ac9673874193a11576c14b7c8 /main.go
parenta871e4f72aab1aa968adc72c4bf2e3e0c530550e (diff)
downloadwebiot-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.
Diffstat (limited to 'main.go')
-rw-r--r--main.go18
1 files changed, 6 insertions, 12 deletions
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)