2020-11-07 02:12:35 +03: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 02:14:59 +03:00
|
|
|
// ServeAdmin will return admin web assets.
|
2020-11-07 02:12:35 +03:00
|
|
|
func ServeAdmin(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Set a cache control max-age header
|
|
|
|
middleware.SetCachingHeaders(w, r)
|
|
|
|
|
2020-11-10 06:52:43 +03:00
|
|
|
// Determine if the requested path is a directory.
|
|
|
|
// If so, append index.html to the request.
|
2020-11-07 02:12:35 +03:00
|
|
|
path := r.URL.Path
|
2020-11-10 06:52:43 +03:00
|
|
|
dirCheck, err := pkger.Stat(path)
|
|
|
|
if dirCheck != nil && err == nil && dirCheck.IsDir() {
|
|
|
|
path = filepath.Join(path, "index.html")
|
2020-11-07 02:12:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
f, err := pkger.Open(path)
|
|
|
|
if err != nil {
|
2021-08-22 04:16:27 +03:00
|
|
|
log.Debugln(err, path)
|
2020-11-15 05:39:53 +03:00
|
|
|
errorHandler(w, http.StatusNotFound)
|
2020-11-07 02:12:35 +03: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 05:39:53 +03:00
|
|
|
if _, err = w.Write(b); err != nil {
|
|
|
|
log.Errorln(err)
|
|
|
|
}
|
2020-11-07 02:12:35 +03:00
|
|
|
}
|
|
|
|
|
2020-11-15 05:39:53 +03:00
|
|
|
func errorHandler(w http.ResponseWriter, status int) {
|
2020-11-07 02:12:35 +03:00
|
|
|
w.WriteHeader(status)
|
|
|
|
}
|