Move `static` to `resources`. Embed them at build time

This commit is contained in:
Deluan 2020-05-01 14:35:57 -04:00 committed by Deluan Quintão
parent b7dcdedf41
commit 1a9663d432
13 changed files with 39 additions and 367 deletions

View File

@ -3,6 +3,7 @@ project_name: navidrome
before:
hooks:
- go-bindata -fs -prefix resources -tags embed -ignore="\\\*.go" -pkg resources -o resources/embedded_gen.go resources/...
- go-bindata -fs -prefix ui/build -tags embed -nocompress -pkg assets -o assets/embedded_gen.go ui/build/...
builds:

View File

@ -35,6 +35,7 @@ RUN GIT_TAG=$(git describe --tags `git rev-list --tags --max-count=1`) && \
GIT_TAG=${GIT_TAG#"tags/"} && \
GIT_SHA=$(git rev-parse --short HEAD) && \
echo "Building version: ${GIT_TAG} (${GIT_SHA})" && \
go-bindata -fs -prefix resources -tags embed -ignore="\\\*.go" -pkg resources -o resources/embedded_gen.go resources/...
go-bindata -fs -prefix ui/build -tags embed -nocompress -pkg assets -o assets/embedded_gen.go ui/build/... && \
go build -ldflags="-X github.com/deluan/navidrome/consts.gitSha=${GIT_SHA} -X github.com/deluan/navidrome/consts.gitTag=${GIT_TAG}" -tags=embed

View File

@ -49,10 +49,6 @@ setup-dev: setup
@lefthook install
.PHONY: setup
static:
cd static && go-bindata -fs -prefix "static" -nocompress -ignore="\\\*.go" -pkg static .
.PHONY: static
Jamstash-master:
wget -N https://github.com/tsquillario/Jamstash/archive/master.zip
unzip -o master.zip
@ -84,6 +80,7 @@ build: check_go_env
buildall: check_env
@(cd ./ui && npm run build)
go-bindata -fs -prefix "resources" -tags embed -ignore="\\\*.go" -pkg resources -o resources/embedded_gen.go resources/...
go-bindata -fs -prefix "ui/build" -tags embed -nocompress -pkg assets -o assets/embedded_gen.go ui/build/...
go build -ldflags="-X github.com/deluan/navidrome/consts.gitSha=$(GIT_SHA) -X github.com/deluan/navidrome/consts.gitTag=$(GIT_TAG)-SNAPSHOT" -tags=embed
.PHONY: buildall

View File

@ -6,7 +6,6 @@ import (
"net/http"
"sync"
"github.com/deluan/navidrome/consts"
"github.com/deluan/navidrome/log"
)
@ -14,7 +13,7 @@ var once sync.Once
func AssetFile() http.FileSystem {
once.Do(func() {
log.Warn("Using external assets from " + consts.UIAssetsLocalPath)
log.Warn("Using external assets from 'ui/build' folder")
})
return http.Dir(consts.UIAssetsLocalPath)
return http.Dir("ui/build")
}

View File

@ -5,11 +5,11 @@ import (
"strings"
"unicode"
"github.com/deluan/navidrome/static"
"github.com/deluan/navidrome/resources"
)
func getBanner() string {
data, _ := static.Asset("banner.txt")
data, _ := resources.Asset("banner.txt")
return strings.TrimRightFunc(string(data), unicode.IsSpace)
}

View File

@ -19,8 +19,6 @@ const (
JWTIssuer = "ND"
DefaultSessionTimeout = 30 * time.Minute
UIAssetsLocalPath = "ui/build"
DevInitialUserName = "admin"
DevInitialName = "Dev Admin"

View File

@ -18,7 +18,7 @@ import (
"github.com/deluan/navidrome/consts"
"github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/model"
"github.com/deluan/navidrome/static"
"github.com/deluan/navidrome/resources"
"github.com/dhowden/tag"
"github.com/disintegration/imaging"
"github.com/djherbis/fscache"
@ -122,7 +122,7 @@ func (c *cover) getCover(ctx context.Context, path string, size int) (reader io.
defer func() {
if err != nil {
log.Warn(ctx, "Error extracting image", "path", path, "size", size, err)
reader, err = static.AssetFile().Open("navidrome-310x310.png")
reader, err = resources.AssetFile().Open("navidrome-310x310.png")
}
}()
var data []byte

28
resources/external.go Normal file
View File

@ -0,0 +1,28 @@
// +build !embed
package resources
import (
"io/ioutil"
"net/http"
"sync"
"github.com/deluan/navidrome/log"
)
var once sync.Once
func Asset(filePath string) ([]byte, error) {
f, err := AssetFile().Open(filePath)
if err != nil {
return nil, err
}
return ioutil.ReadAll(f)
}
func AssetFile() http.FileSystem {
once.Do(func() {
log.Warn("Using external resources from 'resources' folder")
})
return http.Dir("resources")
}

View File

Before

Width:  |  Height:  |  Size: 61 KiB

After

Width:  |  Height:  |  Size: 61 KiB

View File

@ -7,8 +7,8 @@ import (
"github.com/deluan/navidrome/engine"
"github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/model"
"github.com/deluan/navidrome/resources"
"github.com/deluan/navidrome/server/subsonic/responses"
"github.com/deluan/navidrome/static"
"github.com/deluan/navidrome/utils"
)
@ -21,7 +21,7 @@ func NewMediaRetrievalController(cover engine.Cover) *MediaRetrievalController {
}
func (c *MediaRetrievalController) GetAvatar(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
f, err := static.AssetFile().Open("navidrome-310x310.png")
f, err := resources.AssetFile().Open("navidrome-310x310.png")
if err != nil {
log.Error(r, "Image not found", err)
return nil, NewError(responses.ErrorDataNotFound, "Avatar image not found")

File diff suppressed because one or more lines are too long

Binary file not shown.

Before

Width:  |  Height:  |  Size: 22 KiB