refactor: migrate from rice to embed.FS

This commit is contained in:
Oleg Lobanov 2021-03-09 18:59:19 +01:00
parent 0fe34ad224
commit fc5506179a
No known key found for this signature in database
GPG key ID: 7CC64E41212621B0
10 changed files with 42 additions and 140 deletions

View file

@ -2,6 +2,7 @@ package http
import (
"encoding/json"
"io/fs"
"log"
"net/http"
"os"
@ -10,15 +11,13 @@ import (
"strings"
"text/template"
rice "github.com/GeertJohan/go.rice"
"github.com/filebrowser/filebrowser/v2/auth"
"github.com/filebrowser/filebrowser/v2/settings"
"github.com/filebrowser/filebrowser/v2/storage"
"github.com/filebrowser/filebrowser/v2/version"
)
func handleWithStaticData(w http.ResponseWriter, _ *http.Request, d *data, box *rice.Box, file, contentType string) (int, error) {
func handleWithStaticData(w http.ResponseWriter, _ *http.Request, d *data, fSys fs.FS, file, contentType string) (int, error) {
w.Header().Set("Content-Type", contentType)
auther, err := d.store.Auth.Get(d.settings.AuthMethod)
@ -79,14 +78,14 @@ func handleWithStaticData(w http.ResponseWriter, _ *http.Request, d *data, box *
data["Json"] = string(b)
fileContents, err := box.String(file)
fileContents, err := fs.ReadFile(fSys, file)
if err != nil {
if err == os.ErrNotExist {
return http.StatusNotFound, err
}
return http.StatusInternalServerError, err
}
index := template.Must(template.New("index").Delims("[{[", "]}]").Parse(fileContents))
index := template.Must(template.New("index").Delims("[{[", "]}]").Parse(string(fileContents)))
err = index.Execute(w, data)
if err != nil {
return http.StatusInternalServerError, err
@ -95,17 +94,14 @@ func handleWithStaticData(w http.ResponseWriter, _ *http.Request, d *data, box *
return 0, nil
}
func getStaticHandlers(store *storage.Storage, server *settings.Server) (index, static http.Handler) {
box := rice.MustFindBox("../frontend/dist")
handler := http.FileServer(box.HTTPBox())
func getStaticHandlers(store *storage.Storage, server *settings.Server, assetsFs fs.FS) (index, static http.Handler) {
index = handle(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
if r.Method != http.MethodGet {
return http.StatusNotFound, nil
}
w.Header().Set("x-xss-protection", "1; mode=block")
return handleWithStaticData(w, r, d, box, "index.html", "text/html; charset=utf-8")
return handleWithStaticData(w, r, d, assetsFs, "index.html", "text/html; charset=utf-8")
}, "", store, server)
static = handle(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
@ -127,11 +123,11 @@ func getStaticHandlers(store *storage.Storage, server *settings.Server) (index,
}
if !strings.HasSuffix(r.URL.Path, ".js") {
handler.ServeHTTP(w, r)
http.FileServer(http.FS(assetsFs)).ServeHTTP(w, r)
return 0, nil
}
return handleWithStaticData(w, r, d, box, r.URL.Path, "application/javascript; charset=utf-8")
return handleWithStaticData(w, r, d, assetsFs, r.URL.Path, "application/javascript; charset=utf-8")
}, "/static/", store, server)
return index, static