Skip to content

Take control over your live stream video by running it yourself. Streaming + chat out of the box.

Use static servers for serving embedded web assets

Gabe Kangas10ff3d1727d1
diff --git a/controllers/emoji.go b/controllers/emoji.go
index 0661b99ceb..a7de3023f8 100644
--- a/controllers/emoji.go
+++ b/controllers/emoji.go
@@ -10,10 +10,11 @@ import (
"github.com/owncast/owncast/config"
"github.com/owncast/owncast/models"
"github.com/owncast/owncast/static"
- "github.com/owncast/owncast/utils"
log "github.com/sirupsen/logrus"
)
+var emojiStaticServer = http.FileServer(http.FS(static.GetEmoji()))
+
// getCustomEmojiList returns a list of custom emoji either from the cache or from the emoji directory.
func getCustomEmojiList() []models.CustomEmoji {
bundledEmoji := static.GetEmoji()
@@ -45,17 +46,7 @@ func GetCustomEmojiList(w http.ResponseWriter, r *http.Request) {
// GetCustomEmojiImage returns a single emoji image.
func GetCustomEmojiImage(w http.ResponseWriter, r *http.Request) {
- bundledEmoji := static.GetEmoji()
path := strings.TrimPrefix(r.URL.Path, "/img/emoji/")
-
- b, err := fs.ReadFile(bundledEmoji, path)
- if err != nil {
- log.Errorln(err)
- w.WriteHeader(http.StatusInternalServerError)
- return
- }
-
- contentType := "image/jpeg"
- cacheTime := utils.GetCacheDurationSecondsForPath(path)
- writeBytesAsImage(b, contentType, w, cacheTime)
+ r.URL.Path = path
+ emojiStaticServer.ServeHTTP(w, r)
}
diff --git a/controllers/index.go b/controllers/index.go
index 32b16cc56e..ba0958d73f 100644
--- a/controllers/index.go
+++ b/controllers/index.go
@@ -4,23 +4,10 @@ import (
"net/http"
"path/filepath"
- "github.com/owncast/owncast/models"
"github.com/owncast/owncast/router/middleware"
"github.com/owncast/owncast/utils"
)
-// MetadataPage represents a server-rendered web page for bots and web scrapers.
-type MetadataPage struct {
- RequestedURL string
- Image string
- Thumbnail string
- TagsString string
- Summary string
- Name string
- Tags []string
- SocialHandles []models.SocialHandle
-}
-
// IndexHandler handles the default index route.
func IndexHandler(w http.ResponseWriter, r *http.Request) {
middleware.EnableCors(w)
@@ -32,19 +19,6 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) {
return
}
- // If the ETags match then return a StatusNotModified
- // if responseCode := middleware.ProcessEtags(w, r); responseCode != 0 {
- // w.WriteHeader(responseCode)
- // return
- // }
-
- // If this is a directory listing request then return a 404
- // info, err := os.Stat(path.Join(config.WebRoot, r.URL.Path))
- // if err != nil || (info.IsDir() && !isIndexRequest) {
- // w.WriteHeader(http.StatusNotFound)
- // return
- // }
-
// Set a cache control max-age header
middleware.SetCachingHeaders(w, r)
diff --git a/controllers/web.go b/controllers/web.go
index 1bef2cd24a..7b16fe74a3 100644
--- a/controllers/web.go
+++ b/controllers/web.go
@@ -1,59 +1,14 @@
package controllers
import (
- "bytes"
- "io/fs"
"net/http"
- "os"
- "path/filepath"
- "strings"
- "github.com/owncast/owncast/router/middleware"
"github.com/owncast/owncast/static"
- log "github.com/sirupsen/logrus"
)
+var staticServer = http.FileServer(http.FS(static.GetWeb()))
+
// serveWeb will serve web assets.
func serveWeb(w http.ResponseWriter, r *http.Request) {
- // If the ETags match then return a StatusNotModified
- // if responseCode := middleware.ProcessEtags(w, r); responseCode != 0 {
- // w.WriteHeader(responseCode)
- // return
- // }
-
- webFiles := static.GetWeb()
- path := strings.TrimPrefix(r.URL.Path, "/")
-
- // Determine if the requested path is a directory.
- // If so, append index.html to the request.
- if info, err := fs.Stat(webFiles, path); err == nil && info.IsDir() {
- path = filepath.Join(path, "index.html")
- } else if _, err := fs.Stat(webFiles, path+"index.html"); err == nil {
- path = filepath.Join(path, "index.html")
- } else if path == "" {
- path = filepath.Join(path, "index.html")
- }
-
- f, err := webFiles.Open(path)
- if os.IsNotExist(err) {
- w.WriteHeader(http.StatusNotFound)
- return
- }
-
- info, err := f.Stat()
- if os.IsNotExist(err) {
- w.WriteHeader(http.StatusNotFound)
- return
- }
-
- // Set a cache control max-age header
- middleware.SetCachingHeaders(w, r)
- d, err := fs.ReadFile(webFiles, path)
- if err != nil {
- log.Errorln(err)
- w.WriteHeader(http.StatusInternalServerError)
- return
- }
-
- http.ServeContent(w, r, info.Name(), info.ModTime(), bytes.NewReader(d))
+ staticServer.ServeHTTP(w, r)
}
diff --git a/router/router.go b/router/router.go
index f697c4ee9f..ea9f509bb6 100644
--- a/router/router.go
+++ b/router/router.go
@@ -25,12 +25,12 @@ import (
// Start starts the router for the http, ws, and rtmp.
func Start() error {
- // The admin web app.
- http.HandleFunc("/admin", middleware.RequireAdminAuth(controllers.IndexHandler))
-
// The primary web app.
http.HandleFunc("/", controllers.IndexHandler)
+ // The admin web app.
+ http.HandleFunc("/admin", middleware.RequireAdminAuth(controllers.IndexHandler))
+
// Return a single emoji image.
http.HandleFunc("/img/emoji/", middleware.RequireAdminAuth(controllers.GetCustomEmojiImage))