2020-11-07 00:12:35 +01:00
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
2021-10-11 23:56:00 +02:00
|
|
|
"bytes"
|
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"
|
|
|
|
)
|
|
|
|
|
2020-11-13 00:14:59 +01:00
|
|
|
// ServeAdmin will return admin web assets.
|
2020-11-07 00:12:35 +01:00
|
|
|
func ServeAdmin(w http.ResponseWriter, r *http.Request) {
|
2021-10-11 23:56:00 +02:00
|
|
|
adminFiles := static.GetAdmin()
|
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.
|
2021-10-11 23:56:00 +02:00
|
|
|
path := strings.TrimPrefix(r.URL.Path, "/")
|
|
|
|
if strings.HasSuffix(path, "/") {
|
2020-11-10 04:52:43 +01:00
|
|
|
path = filepath.Join(path, "index.html")
|
2020-11-07 00:12:35 +01:00
|
|
|
}
|
|
|
|
|
2021-10-11 23:56:00 +02:00
|
|
|
f, err := adminFiles.Open(path)
|
|
|
|
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)
|
|
|
|
|
|
|
|
d, err := adminFiles.ReadFile(path)
|
|
|
|
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
|
|
|
}
|