Use static servers for serving embedded web assets

This commit is contained in:
Gabe Kangas 2022-06-20 22:12:44 -07:00
parent 18a184eeb7
commit 9c477e16a2
No known key found for this signature in database
GPG key ID: 9A56337728BC81EA
4 changed files with 10 additions and 90 deletions

View file

@ -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)
}

View file

@ -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)

View file

@ -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)
}

View file

@ -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))