Skip to content

Commit fc55061

Browse files
committed
refactor: migrate from rice to embed.FS
1 parent 0fe34ad commit fc55061

File tree

10 files changed

+42
-140
lines changed

10 files changed

+42
-140
lines changed

Diff for: .gitignore

-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ _old
55
rice-box.go
66
.idea/
77
filebrowser
8-
dist/
98

109
.DS_Store
1110
node_modules
12-
/frontend/dist
1311

1412
# local env files
1513
.env.local

Diff for: .goreleaser.yml

-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ project_name: filebrowser
33
env:
44
- GO111MODULE=on
55

6-
before:
7-
hooks:
8-
- make bundle-frontend
9-
106
build:
117
env:
128
- CGO_ENABLED=0

Diff for: Makefile

+1-7
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ $(BIN)/golangci-lint: PACKAGE=github.com/golangci/golangci-lint/cmd/golangci-lin
3737
GOIMPORTS = $(BIN)/goimports
3838
$(BIN)/goimports: PACKAGE=golang.org/x/tools/cmd/[email protected]
3939

40-
RICE = $(BIN)/rice
41-
$(BIN)/rice: PACKAGE=github.com/GeertJohan/go.rice/[email protected]
42-
4340
## build: Build
4441
.PHONY: build
4542
build: | build-frontend build-backend ; $(info $(M) building…)
@@ -51,12 +48,9 @@ build-frontend: | ; $(info $(M) building frontend…)
5148

5249
## build-backend: Build backend
5350
.PHONY: build-backend
54-
build-backend: bundle-frontend | ; $(info $(M) building backend…)
51+
build-backend: | ; $(info $(M) building backend…)
5552
$Q $(GO) build -ldflags '$(LDFLAGS)' -o filebrowser
5653

57-
bundle-frontend: | $(RICE) ; $(info $(M) building backend…)
58-
$Q cd ./http && rm -rf rice-box.go && $(RICE) embed-go
59-
6054
## test: Run all tests
6155
.PHONY: test
6256
test: | test-frontend test-backend ; $(info $(M) running tests…)

Diff for: cmd/root.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"crypto/tls"
55
"errors"
6+
"io/fs"
67
"io/ioutil"
78
"log"
89
"net"
@@ -22,6 +23,7 @@ import (
2223

2324
"github.com/filebrowser/filebrowser/v2/auth"
2425
"github.com/filebrowser/filebrowser/v2/diskcache"
26+
"github.com/filebrowser/filebrowser/v2/frontend"
2527
fbhttp "github.com/filebrowser/filebrowser/v2/http"
2628
"github.com/filebrowser/filebrowser/v2/img"
2729
"github.com/filebrowser/filebrowser/v2/settings"
@@ -168,7 +170,12 @@ user created with the credentials from options "username" and "password".`,
168170
signal.Notify(sigc, os.Interrupt, syscall.SIGTERM)
169171
go cleanupHandler(listener, sigc)
170172

171-
handler, err := fbhttp.NewHandler(imgSvc, fileCache, d.store, server)
173+
assetsFs, err := fs.Sub(frontend.Assets(), "dist")
174+
if err != nil {
175+
panic(err)
176+
}
177+
178+
handler, err := fbhttp.NewHandler(imgSvc, fileCache, d.store, server, assetsFs)
172179
checkErr(err)
173180

174181
defer listener.Close()

Diff for: frontend/assets.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package frontend
2+
3+
import "embed"
4+
5+
//go:embed dist/*
6+
var assets embed.FS
7+
8+
func Assets() embed.FS {
9+
return assets
10+
}

Diff for: frontend/dist/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Ignore everything in this directory
2+
*
3+
# Except this file
4+
!.gitignore

Diff for: go.mod

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
module github.com/filebrowser/filebrowser/v2
22

3+
go 1.16
4+
35
require (
46
github.com/DataDog/zstd v1.4.0 // indirect
5-
github.com/GeertJohan/go.rice v1.0.0
67
github.com/Sereal/Sereal v0.0.0-20190430203904-6faf9605eb56 // indirect
78
github.com/asdine/storm v2.1.2+incompatible
89
github.com/caddyserver/caddy v1.0.3
9-
github.com/daaku/go.zipexe v1.0.1 // indirect
1010
github.com/dgrijalva/jwt-go v3.2.0+incompatible
1111
github.com/disintegration/imaging v1.6.2
1212
github.com/dsnet/compress v0.0.1 // indirect
@@ -39,5 +39,3 @@ require (
3939
gopkg.in/natefinch/lumberjack.v2 v2.0.0
4040
gopkg.in/yaml.v2 v2.2.7
4141
)
42-
43-
go 1.14

Diff for: go.sum

-108
Large diffs are not rendered by default.

Diff for: http/http.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package http
22

33
import (
4+
"io/fs"
45
"net/http"
56

67
"github.com/gorilla/mux"
@@ -14,11 +15,17 @@ type modifyRequest struct {
1415
Which []string `json:"which"` // Answer to: which fields?
1516
}
1617

17-
func NewHandler(imgSvc ImgService, fileCache FileCache, store *storage.Storage, server *settings.Server) (http.Handler, error) {
18+
func NewHandler(
19+
imgSvc ImgService,
20+
fileCache FileCache,
21+
store *storage.Storage,
22+
server *settings.Server,
23+
assetsFs fs.FS,
24+
) (http.Handler, error) {
1825
server.Clean()
1926

2027
r := mux.NewRouter()
21-
index, static := getStaticHandlers(store, server)
28+
index, static := getStaticHandlers(store, server, assetsFs)
2229

2330
// NOTE: This fixes the issue where it would redirect if people did not put a
2431
// trailing slash in the end. I hate this decision since this allows some awful

Diff for: http/static.go

+8-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package http
22

33
import (
44
"encoding/json"
5+
"io/fs"
56
"log"
67
"net/http"
78
"os"
@@ -10,15 +11,13 @@ import (
1011
"strings"
1112
"text/template"
1213

13-
rice "github.com/GeertJohan/go.rice"
14-
1514
"github.com/filebrowser/filebrowser/v2/auth"
1615
"github.com/filebrowser/filebrowser/v2/settings"
1716
"github.com/filebrowser/filebrowser/v2/storage"
1817
"github.com/filebrowser/filebrowser/v2/version"
1918
)
2019

21-
func handleWithStaticData(w http.ResponseWriter, _ *http.Request, d *data, box *rice.Box, file, contentType string) (int, error) {
20+
func handleWithStaticData(w http.ResponseWriter, _ *http.Request, d *data, fSys fs.FS, file, contentType string) (int, error) {
2221
w.Header().Set("Content-Type", contentType)
2322

2423
auther, err := d.store.Auth.Get(d.settings.AuthMethod)
@@ -79,14 +78,14 @@ func handleWithStaticData(w http.ResponseWriter, _ *http.Request, d *data, box *
7978

8079
data["Json"] = string(b)
8180

82-
fileContents, err := box.String(file)
81+
fileContents, err := fs.ReadFile(fSys, file)
8382
if err != nil {
8483
if err == os.ErrNotExist {
8584
return http.StatusNotFound, err
8685
}
8786
return http.StatusInternalServerError, err
8887
}
89-
index := template.Must(template.New("index").Delims("[{[", "]}]").Parse(fileContents))
88+
index := template.Must(template.New("index").Delims("[{[", "]}]").Parse(string(fileContents)))
9089
err = index.Execute(w, data)
9190
if err != nil {
9291
return http.StatusInternalServerError, err
@@ -95,17 +94,14 @@ func handleWithStaticData(w http.ResponseWriter, _ *http.Request, d *data, box *
9594
return 0, nil
9695
}
9796

98-
func getStaticHandlers(store *storage.Storage, server *settings.Server) (index, static http.Handler) {
99-
box := rice.MustFindBox("../frontend/dist")
100-
handler := http.FileServer(box.HTTPBox())
101-
97+
func getStaticHandlers(store *storage.Storage, server *settings.Server, assetsFs fs.FS) (index, static http.Handler) {
10298
index = handle(func(w http.ResponseWriter, r *http.Request, d *data) (int, error) {
10399
if r.Method != http.MethodGet {
104100
return http.StatusNotFound, nil
105101
}
106102

107103
w.Header().Set("x-xss-protection", "1; mode=block")
108-
return handleWithStaticData(w, r, d, box, "index.html", "text/html; charset=utf-8")
104+
return handleWithStaticData(w, r, d, assetsFs, "index.html", "text/html; charset=utf-8")
109105
}, "", store, server)
110106

111107
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,
127123
}
128124

129125
if !strings.HasSuffix(r.URL.Path, ".js") {
130-
handler.ServeHTTP(w, r)
126+
http.FileServer(http.FS(assetsFs)).ServeHTTP(w, r)
131127
return 0, nil
132128
}
133129

134-
return handleWithStaticData(w, r, d, box, r.URL.Path, "application/javascript; charset=utf-8")
130+
return handleWithStaticData(w, r, d, assetsFs, r.URL.Path, "application/javascript; charset=utf-8")
135131
}, "/static/", store, server)
136132

137133
return index, static

0 commit comments

Comments
 (0)