Use go:embed for statics (#92)

This commit is contained in:
Antonio Navarro Perez 2021-11-21 18:31:47 +01:00 committed by GitHub
parent ddda39b22a
commit 1d769673ca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 13 additions and 76 deletions

View file

@ -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))
}

View file

@ -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")
}
}

View file

@ -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
}

2
go.mod
View file

@ -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

6
go.sum
View file

@ -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=

View file

@ -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)
}

View file

@ -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