diff --git a/cmd/api/api.go b/cmd/api/api.go index c5031c9d..7b95524b 100644 --- a/cmd/api/api.go +++ b/cmd/api/api.go @@ -22,7 +22,6 @@ func Init() { // default config cfg.Mod.Listen = ":1984" - cfg.Mod.StaticDir = "www" // load config from YAML app.LoadConfig(&cfg) @@ -34,10 +33,7 @@ func Init() { basePath = cfg.Mod.BasePath log = app.GetLogger("api") - if cfg.Mod.StaticDir != "" { - fileServer = http.FileServer(http.Dir(cfg.Mod.StaticDir)) - HandleFunc("/", fileServerHandlder) - } + initStatic(cfg.Mod.StaticDir) HandleFunc("/api/frame.mp4", frameHandler) HandleFunc("/api/frame.raw", frameHandler) @@ -70,17 +66,9 @@ func HandleWS(msgType string, handler WSHandler) { } var basePath string -var fileServer http.Handler var log zerolog.Logger var wsHandlers = make(map[string]WSHandler) -func fileServerHandlder(w http.ResponseWriter, r *http.Request) { - if basePath != "" { - r.URL.Path = r.URL.Path[len(basePath):] - } - fileServer.ServeHTTP(w, r) -} - func statsHandler(w http.ResponseWriter, _ *http.Request) { v := map[string]interface{}{ "streams": streams.All(), diff --git a/cmd/api/static.go b/cmd/api/static.go new file mode 100644 index 00000000..ea3b9f4d --- /dev/null +++ b/cmd/api/static.go @@ -0,0 +1,25 @@ +package api + +import ( + "github.com/AlexxIT/go2rtc/www" + "net/http" +) + +func initStatic(staticDir string) { + var root http.FileSystem + if staticDir != "" { + root = http.Dir(staticDir) + } else { + root = http.FS(www.Static) + } + + fileServer := http.FileServer(root) + + HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + if basePath != "" { + r.URL.Path = r.URL.Path[len(basePath):] + } + + fileServer.ServeHTTP(w, r) + }) +} diff --git a/www/index.html b/www/index.html index 39e70eac..485e8f90 100644 --- a/www/index.html +++ b/www/index.html @@ -15,8 +15,7 @@ ); const header = document.getElementById('header'); - header.innerHTML = `stats` + - `webcam`; + header.innerHTML = `stats`; const links = [ 'webrtc-async', diff --git a/www/static.go b/www/static.go new file mode 100644 index 00000000..80e6b169 --- /dev/null +++ b/www/static.go @@ -0,0 +1,6 @@ +package www + +import "embed" + +//go:embed *.html +var Static embed.FS diff --git a/www/webrtc-async.html b/www/webrtc-async.html index 5f6a7b36..c0fc9661 100644 --- a/www/webrtc-async.html +++ b/www/webrtc-async.html @@ -21,11 +21,8 @@ background: black; } - - -