From 9a38033b65d35dcacf0fb7645263d4b7642dd304 Mon Sep 17 00:00:00 2001 From: bel Date: Mon, 10 Apr 2023 18:38:38 -0600 Subject: [PATCH 1/2] RAW_WS serves all static files --- src/device/input/raw/ws.go | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/device/input/raw/ws.go b/src/device/input/raw/ws.go index b69d680..55a648a 100644 --- a/src/device/input/raw/ws.go +++ b/src/device/input/raw/ws.go @@ -2,6 +2,7 @@ package raw import ( "context" + "embed" _ "embed" "fmt" "log" @@ -9,6 +10,7 @@ import ( "net/http/httputil" "net/url" "os" + "path" "strings" "github.com/gorilla/websocket" @@ -75,16 +77,13 @@ func (ws WS) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (ws WS) serveHTTP(w http.ResponseWriter, r *http.Request) error { switch r.URL.Path { - case "/": - return ws.serveIndex(w, r) case "/api/ws": return ws.serveWS(w, r) } if strings.HasPrefix(r.URL.Path, "/proxy") { return ws.serveProxy(w, r) } - http.NotFound(w, r) - return nil + return ws.serveStaticFile(w, r) } func (ws WS) serveProxy(w http.ResponseWriter, r *http.Request) error { @@ -101,16 +100,24 @@ func (ws WS) serveProxy(w http.ResponseWriter, r *http.Request) error { return nil } -//go:embed public/root.html -var rootHTML string +//go:embed public/* +var staticFiles embed.FS -func (ws WS) serveIndex(w http.ResponseWriter, r *http.Request) error { - v := rootHTML +func (ws WS) serveStaticFile(w http.ResponseWriter, r *http.Request) error { + log.Println("serveStaticFile", 0, r.URL.Path) if FlagWSDebug { b, _ := os.ReadFile("src/device/input/raw/public/root.html") - v = string(b) + w.Write(b) + return nil } - w.Write([]byte(v)) + log.Println("serveStaticFile", 1, r.URL.Path) + if r.URL.Path == "/" { + r.URL.Path = "root.html" + } + log.Println("serveStaticFile", 2, r.URL.Path) + r.URL.Path = path.Join("public", r.URL.Path) + log.Println("serveStaticFile", 3, r.URL.Path) + http.FileServer(http.FS(staticFiles)).ServeHTTP(w, r) return nil } From 41a39c40d0d627b67951c05da9ca2e48047c9cb2 Mon Sep 17 00:00:00 2001 From: bel Date: Mon, 10 Apr 2023 18:40:43 -0600 Subject: [PATCH 2/2] oops debug logs --- src/device/input/raw/ws.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/device/input/raw/ws.go b/src/device/input/raw/ws.go index 55a648a..47dfcc9 100644 --- a/src/device/input/raw/ws.go +++ b/src/device/input/raw/ws.go @@ -104,19 +104,15 @@ func (ws WS) serveProxy(w http.ResponseWriter, r *http.Request) error { var staticFiles embed.FS func (ws WS) serveStaticFile(w http.ResponseWriter, r *http.Request) error { - log.Println("serveStaticFile", 0, r.URL.Path) if FlagWSDebug { b, _ := os.ReadFile("src/device/input/raw/public/root.html") w.Write(b) return nil } - log.Println("serveStaticFile", 1, r.URL.Path) if r.URL.Path == "/" { r.URL.Path = "root.html" } - log.Println("serveStaticFile", 2, r.URL.Path) r.URL.Path = path.Join("public", r.URL.Path) - log.Println("serveStaticFile", 3, r.URL.Path) http.FileServer(http.FS(staticFiles)).ServeHTTP(w, r) return nil }