Use static servers for serving embedded web assets
This commit is contained in:
parent
18a184eeb7
commit
9c477e16a2
@ -10,10 +10,11 @@ import (
|
|||||||
"github.com/owncast/owncast/config"
|
"github.com/owncast/owncast/config"
|
||||||
"github.com/owncast/owncast/models"
|
"github.com/owncast/owncast/models"
|
||||||
"github.com/owncast/owncast/static"
|
"github.com/owncast/owncast/static"
|
||||||
"github.com/owncast/owncast/utils"
|
|
||||||
log "github.com/sirupsen/logrus"
|
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.
|
// getCustomEmojiList returns a list of custom emoji either from the cache or from the emoji directory.
|
||||||
func getCustomEmojiList() []models.CustomEmoji {
|
func getCustomEmojiList() []models.CustomEmoji {
|
||||||
bundledEmoji := static.GetEmoji()
|
bundledEmoji := static.GetEmoji()
|
||||||
@ -45,17 +46,7 @@ func GetCustomEmojiList(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// GetCustomEmojiImage returns a single emoji image.
|
// GetCustomEmojiImage returns a single emoji image.
|
||||||
func GetCustomEmojiImage(w http.ResponseWriter, r *http.Request) {
|
func GetCustomEmojiImage(w http.ResponseWriter, r *http.Request) {
|
||||||
bundledEmoji := static.GetEmoji()
|
|
||||||
path := strings.TrimPrefix(r.URL.Path, "/img/emoji/")
|
path := strings.TrimPrefix(r.URL.Path, "/img/emoji/")
|
||||||
|
r.URL.Path = path
|
||||||
b, err := fs.ReadFile(bundledEmoji, path)
|
emojiStaticServer.ServeHTTP(w, r)
|
||||||
if err != nil {
|
|
||||||
log.Errorln(err)
|
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
contentType := "image/jpeg"
|
|
||||||
cacheTime := utils.GetCacheDurationSecondsForPath(path)
|
|
||||||
writeBytesAsImage(b, contentType, w, cacheTime)
|
|
||||||
}
|
}
|
||||||
|
@ -4,23 +4,10 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/owncast/owncast/models"
|
|
||||||
"github.com/owncast/owncast/router/middleware"
|
"github.com/owncast/owncast/router/middleware"
|
||||||
"github.com/owncast/owncast/utils"
|
"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.
|
// IndexHandler handles the default index route.
|
||||||
func IndexHandler(w http.ResponseWriter, r *http.Request) {
|
func IndexHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
middleware.EnableCors(w)
|
middleware.EnableCors(w)
|
||||||
@ -32,19 +19,6 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
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
|
// Set a cache control max-age header
|
||||||
middleware.SetCachingHeaders(w, r)
|
middleware.SetCachingHeaders(w, r)
|
||||||
|
|
||||||
|
@ -1,59 +1,14 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"io/fs"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/owncast/owncast/router/middleware"
|
|
||||||
"github.com/owncast/owncast/static"
|
"github.com/owncast/owncast/static"
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var staticServer = http.FileServer(http.FS(static.GetWeb()))
|
||||||
|
|
||||||
// serveWeb will serve web assets.
|
// serveWeb will serve web assets.
|
||||||
func serveWeb(w http.ResponseWriter, r *http.Request) {
|
func serveWeb(w http.ResponseWriter, r *http.Request) {
|
||||||
// If the ETags match then return a StatusNotModified
|
staticServer.ServeHTTP(w, r)
|
||||||
// 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))
|
|
||||||
}
|
}
|
||||||
|
@ -25,12 +25,12 @@ import (
|
|||||||
|
|
||||||
// Start starts the router for the http, ws, and rtmp.
|
// Start starts the router for the http, ws, and rtmp.
|
||||||
func Start() error {
|
func Start() error {
|
||||||
// The admin web app.
|
|
||||||
http.HandleFunc("/admin", middleware.RequireAdminAuth(controllers.IndexHandler))
|
|
||||||
|
|
||||||
// The primary web app.
|
// The primary web app.
|
||||||
http.HandleFunc("/", controllers.IndexHandler)
|
http.HandleFunc("/", controllers.IndexHandler)
|
||||||
|
|
||||||
|
// The admin web app.
|
||||||
|
http.HandleFunc("/admin", middleware.RequireAdminAuth(controllers.IndexHandler))
|
||||||
|
|
||||||
// Return a single emoji image.
|
// Return a single emoji image.
|
||||||
http.HandleFunc("/img/emoji/", middleware.RequireAdminAuth(controllers.GetCustomEmojiImage))
|
http.HandleFunc("/img/emoji/", middleware.RequireAdminAuth(controllers.GetCustomEmojiImage))
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user