2022-06-20 00:30:32 +02:00
|
|
|
package controllers
|
2020-11-07 00:12:35 +01:00
|
|
|
|
|
|
|
import (
|
2021-10-11 23:56:00 +02:00
|
|
|
"bytes"
|
2021-10-12 21:47:58 +02:00
|
|
|
"io/fs"
|
2020-11-07 00:12:35 +01:00
|
|
|
"net/http"
|
2021-10-11 23:56:00 +02:00
|
|
|
"os"
|
2020-11-07 00:12:35 +01:00
|
|
|
"path/filepath"
|
2021-10-11 23:56:00 +02:00
|
|
|
"strings"
|
2020-11-07 00:12:35 +01:00
|
|
|
|
|
|
|
"github.com/owncast/owncast/router/middleware"
|
2021-10-11 23:56:00 +02:00
|
|
|
"github.com/owncast/owncast/static"
|
2020-11-07 00:12:35 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2022-06-20 00:30:32 +02:00
|
|
|
// serveWeb will serve web assets.
|
|
|
|
func serveWeb(w http.ResponseWriter, r *http.Request) {
|
2021-10-12 21:47:58 +02:00
|
|
|
// If the ETags match then return a StatusNotModified
|
2022-06-21 06:43:53 +02:00
|
|
|
// if responseCode := middleware.ProcessEtags(w, r); responseCode != 0 {
|
|
|
|
// w.WriteHeader(responseCode)
|
|
|
|
// return
|
|
|
|
// }
|
2021-10-12 21:47:58 +02:00
|
|
|
|
2022-06-20 00:30:32 +02:00
|
|
|
webFiles := static.GetWeb()
|
2022-06-21 06:43:53 +02:00
|
|
|
path := strings.TrimPrefix(r.URL.Path, "/")
|
2020-11-07 00:12:35 +01:00
|
|
|
|
2020-11-10 04:52:43 +01:00
|
|
|
// Determine if the requested path is a directory.
|
|
|
|
// If so, append index.html to the request.
|
2022-06-20 00:30:32 +02:00
|
|
|
if info, err := fs.Stat(webFiles, path); err == nil && info.IsDir() {
|
2020-11-10 04:52:43 +01:00
|
|
|
path = filepath.Join(path, "index.html")
|
2022-06-20 00:30:32 +02:00
|
|
|
} else if _, err := fs.Stat(webFiles, path+"index.html"); err == nil {
|
|
|
|
path = filepath.Join(path, "index.html")
|
|
|
|
} else if path == "" {
|
2021-10-12 21:47:58 +02:00
|
|
|
path = filepath.Join(path, "index.html")
|
2020-11-07 00:12:35 +01:00
|
|
|
}
|
|
|
|
|
2022-06-20 00:30:32 +02:00
|
|
|
f, err := webFiles.Open(path)
|
2021-10-11 23:56:00 +02:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2020-11-07 00:12:35 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-11 23:56:00 +02:00
|
|
|
info, err := f.Stat()
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2020-11-07 00:12:35 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-11 23:56:00 +02:00
|
|
|
// Set a cache control max-age header
|
|
|
|
middleware.SetCachingHeaders(w, r)
|
2022-06-21 06:43:53 +02:00
|
|
|
d, err := fs.ReadFile(webFiles, path)
|
2021-10-11 23:56:00 +02:00
|
|
|
if err != nil {
|
2020-11-15 03:39:53 +01:00
|
|
|
log.Errorln(err)
|
2021-10-11 23:56:00 +02:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
2020-11-15 03:39:53 +01:00
|
|
|
}
|
2020-11-07 00:12:35 +01:00
|
|
|
|
2021-10-11 23:56:00 +02:00
|
|
|
http.ServeContent(w, r, info.Name(), info.ModTime(), bytes.NewReader(d))
|
2020-11-07 00:12:35 +01:00
|
|
|
}
|