* Replace pkger with go:embed for bundling the admin. Closes #844 * Remove references to pkged.go * Point tests to use an updated version of Go * Add comment to new exported function * Cleanup * Add a dummy pkged.go to alert people to stop using it. * Add simple browser test to make sure the admin is available and renders * Don't panic
This commit is contained in:
@@ -1,49 +1,50 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"mime"
|
||||
"bytes"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/markbates/pkger"
|
||||
"github.com/owncast/owncast/router/middleware"
|
||||
"github.com/owncast/owncast/static"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// ServeAdmin will return admin web assets.
|
||||
func ServeAdmin(w http.ResponseWriter, r *http.Request) {
|
||||
// Set a cache control max-age header
|
||||
middleware.SetCachingHeaders(w, r)
|
||||
adminFiles := static.GetAdmin()
|
||||
|
||||
// Determine if the requested path is a directory.
|
||||
// If so, append index.html to the request.
|
||||
path := r.URL.Path
|
||||
dirCheck, err := pkger.Stat(path)
|
||||
if dirCheck != nil && err == nil && dirCheck.IsDir() {
|
||||
path := strings.TrimPrefix(r.URL.Path, "/")
|
||||
if strings.HasSuffix(path, "/") {
|
||||
path = filepath.Join(path, "index.html")
|
||||
}
|
||||
|
||||
f, err := pkger.Open(path)
|
||||
if err != nil {
|
||||
log.Debugln(err, path)
|
||||
errorHandler(w, http.StatusNotFound)
|
||||
f, err := adminFiles.Open(path)
|
||||
if os.IsNotExist(err) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
b, err := ioutil.ReadAll(f)
|
||||
if err != nil {
|
||||
log.Warnln(err)
|
||||
info, err := f.Stat()
|
||||
|
||||
if os.IsNotExist(err) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
mimeType := mime.TypeByExtension(filepath.Ext(path))
|
||||
w.Header().Set("Content-Type", mimeType)
|
||||
if _, err = w.Write(b); err != nil {
|
||||
// Set a cache control max-age header
|
||||
middleware.SetCachingHeaders(w, r)
|
||||
|
||||
d, err := adminFiles.ReadFile(path)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func errorHandler(w http.ResponseWriter, status int) {
|
||||
w.WriteHeader(status)
|
||||
http.ServeContent(w, r, info.Name(), info.ModTime(), bytes.NewReader(d))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user