RAW_WS serves all static files
parent
6a4ad5ec36
commit
9a38033b65
|
|
@ -2,6 +2,7 @@ package raw
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"embed"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
@ -9,6 +10,7 @@ import (
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"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 {
|
func (ws WS) serveHTTP(w http.ResponseWriter, r *http.Request) error {
|
||||||
switch r.URL.Path {
|
switch r.URL.Path {
|
||||||
case "/":
|
|
||||||
return ws.serveIndex(w, r)
|
|
||||||
case "/api/ws":
|
case "/api/ws":
|
||||||
return ws.serveWS(w, r)
|
return ws.serveWS(w, r)
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(r.URL.Path, "/proxy") {
|
if strings.HasPrefix(r.URL.Path, "/proxy") {
|
||||||
return ws.serveProxy(w, r)
|
return ws.serveProxy(w, r)
|
||||||
}
|
}
|
||||||
http.NotFound(w, r)
|
return ws.serveStaticFile(w, r)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ws WS) serveProxy(w http.ResponseWriter, r *http.Request) error {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//go:embed public/root.html
|
//go:embed public/*
|
||||||
var rootHTML string
|
var staticFiles embed.FS
|
||||||
|
|
||||||
func (ws WS) serveIndex(w http.ResponseWriter, r *http.Request) error {
|
func (ws WS) serveStaticFile(w http.ResponseWriter, r *http.Request) error {
|
||||||
v := rootHTML
|
log.Println("serveStaticFile", 0, r.URL.Path)
|
||||||
if FlagWSDebug {
|
if FlagWSDebug {
|
||||||
b, _ := os.ReadFile("src/device/input/raw/public/root.html")
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue