From 1d769673ca26c1bb97bdd3aea4a78d086f7c6f02 Mon Sep 17 00:00:00 2001 From: Antonio Navarro Perez Date: Sun, 21 Nov 2021 18:31:47 +0100 Subject: [PATCH] Use go:embed for statics (#92) --- binary_fs.go | 33 ----------------------------- build_tools/assets_generate/main.go | 20 ----------------- config/handler.go | 2 +- go.mod | 2 -- go.sum | 6 ------ http/http.go | 10 +++++---- http_fs.go | 16 ++++++-------- 7 files changed, 13 insertions(+), 76 deletions(-) delete mode 100644 binary_fs.go delete mode 100644 build_tools/assets_generate/main.go diff --git a/binary_fs.go b/binary_fs.go deleted file mode 100644 index 2c7c8b7..0000000 --- a/binary_fs.go +++ /dev/null @@ -1,33 +0,0 @@ -package distribyted - -import ( - "net/http" - "path/filepath" - "strings" -) - -type binaryFileSystem struct { - fs http.FileSystem - base string -} - -func NewBinaryFileSystem(fs http.FileSystem, base string) *binaryFileSystem { - return &binaryFileSystem{ - fs: fs, - base: base, - } -} - -func (fs *binaryFileSystem) Exists(prefix string, filepath string) bool { - if p := strings.TrimPrefix(filepath, prefix); len(p) < len(filepath) { - if _, err := fs.Open(p); err != nil { - return false - } - return true - } - return false -} - -func (fs *binaryFileSystem) Open(name string) (http.File, error) { - return fs.fs.Open(filepath.Join(fs.base, name)) -} diff --git a/build_tools/assets_generate/main.go b/build_tools/assets_generate/main.go deleted file mode 100644 index 3009d46..0000000 --- a/build_tools/assets_generate/main.go +++ /dev/null @@ -1,20 +0,0 @@ -// +build ignore - -package main - -import ( - "github.com/distribyted/distribyted" - "github.com/rs/zerolog/log" - "github.com/shurcooL/vfsgen" -) - -func main() { - err := vfsgen.Generate(distribyted.HttpFS, vfsgen.Options{ - BuildTags: "release", - VariableName: "HttpFS", - PackageName: "distribyted", - }) - if err != nil { - log.Fatal().Err(err).Msg("problem generating static files") - } -} diff --git a/config/handler.go b/config/handler.go index 7931816..d47f46c 100644 --- a/config/handler.go +++ b/config/handler.go @@ -22,7 +22,7 @@ func NewHandler(path string) *Handler { } func (c *Handler) createFromTemplateFile() ([]byte, error) { - t, err := distribyted.HttpFS.Open("/templates/config_template.yaml") + t, err := distribyted.Templates.Open("templates/config_template.yaml") if err != nil { return nil, err } diff --git a/go.mod b/go.mod index efadac3..cbbe0c8 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,6 @@ require ( github.com/dgraph-io/badger/v3 v3.2103.2 github.com/elliotchance/orderedmap v1.4.0 // indirect github.com/fsnotify/fsnotify v1.5.1 - github.com/gin-contrib/static v0.0.1 github.com/gin-gonic/gin v1.7.4 github.com/go-playground/validator/v10 v10.9.0 // indirect github.com/golang/protobuf v1.5.2 // indirect @@ -96,7 +95,6 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rs/dnscache v0.0.0-20211102005908-e0241e321417 // indirect github.com/ryszard/goskiplist v0.0.0-20150312221310-2dfbae5fcf46 // indirect - github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect github.com/ugorji/go/codec v1.1.7 // indirect github.com/willf/bitset v1.1.11 // indirect diff --git a/go.sum b/go.sum index ef17a34..2318ffb 100644 --- a/go.sum +++ b/go.sum @@ -132,9 +132,6 @@ github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWp github.com/fsnotify/fsnotify v1.5.1/go.mod h1:T3375wBYaZdLLcVNkcVbzGHY7f1l/uK5T5Ai1i3InKU= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= -github.com/gin-contrib/static v0.0.1 h1:JVxuvHPuUfkoul12N7dtQw7KRn/pSMq7Ue1Va9Swm1U= -github.com/gin-contrib/static v0.0.1/go.mod h1:CSxeF+wep05e0kCOsqWdAWbSszmc31zTIbD8TvWl7Hs= -github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M= github.com/gin-gonic/gin v1.7.4 h1:QmUZXrvJ9qZ3GfWvQ+2wnW/1ePrTEJqPKMYEU3lD/DM= github.com/gin-gonic/gin v1.7.4/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY= github.com/glycerine/go-unsnap-stream v0.0.0-20180323001048-9f0cb55181dd/go.mod h1:/20jfyN9Y5QPEAprSgKAUr+glWDY39ZiUEAYOEv5dsE= @@ -155,7 +152,6 @@ github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho= github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA= -github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI= github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= github.com/go-playground/validator/v10 v10.9.0 h1:NgTtmN58D0m8+UuxtYmGztBJB7VnPgjj221I1QHci2A= github.com/go-playground/validator/v10 v10.9.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos= @@ -365,8 +361,6 @@ github.com/sclevine/agouti v3.0.0+incompatible/go.mod h1:b4WX9W9L1sfQKXeJf1mUTLZ github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 h1:bUGsEnyNbVPw06Bs80sCeARAlK8lhwqGyi6UT8ymuGk= github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= -github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546 h1:pXY9qYc/MP5zdvqWEUH6SjNiu7VhSjuVFTFiTcphaLU= -github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= diff --git a/http/http.go b/http/http.go index 13e3f77..7cc4102 100644 --- a/http/http.go +++ b/http/http.go @@ -2,12 +2,12 @@ package http import ( "fmt" + "net/http" "github.com/anacrolix/missinggo/v2/filecache" "github.com/distribyted/distribyted" "github.com/distribyted/distribyted/config" "github.com/distribyted/distribyted/torrent" - "github.com/gin-contrib/static" "github.com/gin-gonic/gin" "github.com/rs/zerolog/log" "github.com/shurcooL/httpfs/html/vfstemplate" @@ -19,9 +19,11 @@ func New(fc *filecache.Cache, ss *torrent.Stats, s *torrent.Service, ch *config. r.Use(gin.Recovery()) r.Use(gin.ErrorLogger()) - assets := distribyted.NewBinaryFileSystem(distribyted.HttpFS, "/assets") - r.Use(static.Serve("/assets", assets)) - t, err := vfstemplate.ParseGlob(distribyted.HttpFS, nil, "/templates/*") + r.GET("/assets/*filepath", func(c *gin.Context) { + c.FileFromFS(c.Request.URL.Path, http.FS(distribyted.Assets)) + }) + + t, err := vfstemplate.ParseGlob(http.FS(distribyted.Templates), nil, "/templates/*") if err != nil { return fmt.Errorf("error parsing html: %w", err) } diff --git a/http_fs.go b/http_fs.go index a55c3c3..0b9bc21 100644 --- a/http_fs.go +++ b/http_fs.go @@ -1,15 +1,11 @@ -// +build !release - package distribyted -//go:generate go run ./build_tools/assets_generate/main.go import ( - "net/http" - - "github.com/shurcooL/httpfs/union" + "embed" ) -var HttpFS = union.New(map[string]http.FileSystem{ - "/assets": http.Dir("assets"), - "/templates": http.Dir("templates"), -}) +//go:embed assets +var Assets embed.FS + +//go:embed templates +var Templates embed.FS