2020-11-07 00:12:35 +01:00
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"mime"
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/markbates/pkger"
|
|
|
|
"github.com/owncast/owncast/router/middleware"
|
|
|
|
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) {
|
|
|
|
// Set a cache control max-age header
|
|
|
|
middleware.SetCachingHeaders(w, r)
|
|
|
|
|
2020-11-10 04:52:43 +01:00
|
|
|
// Determine if the requested path is a directory.
|
|
|
|
// If so, append index.html to the request.
|
2020-11-07 00:12:35 +01:00
|
|
|
path := r.URL.Path
|
2020-11-10 04:52:43 +01:00
|
|
|
dirCheck, err := pkger.Stat(path)
|
|
|
|
if dirCheck != nil && err == nil && dirCheck.IsDir() {
|
|
|
|
path = filepath.Join(path, "index.html")
|
2020-11-07 00:12:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
f, err := pkger.Open(path)
|
|
|
|
if err != nil {
|
2021-08-22 03:16:27 +02:00
|
|
|
log.Debugln(err, path)
|
2020-11-15 03:39:53 +01:00
|
|
|
errorHandler(w, http.StatusNotFound)
|
2020-11-07 00:12:35 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnln(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mimeType := mime.TypeByExtension(filepath.Ext(path))
|
|
|
|
w.Header().Set("Content-Type", mimeType)
|
2020-11-15 03:39:53 +01:00
|
|
|
if _, err = w.Write(b); err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
}
|
2020-11-07 00:12:35 +01:00
|
|
|
}
|
|
|
|
|
2020-11-15 03:39:53 +01:00
|
|
|
func errorHandler(w http.ResponseWriter, status int) {
|
2020-11-07 00:12:35 +01:00
|
|
|
w.WriteHeader(status)
|
|
|
|
}
|