Skip to content

Commit

Permalink
Move static to resources. Embed them at build time
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed May 2, 2020
1 parent b7dcded commit 1a9663d
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 367 deletions.
1 change: 1 addition & 0 deletions .goreleaser.yml
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Expand Up @@ -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

Expand Down
5 changes: 1 addition & 4 deletions Makefile
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions assets/external.go
Expand Up @@ -6,15 +6,14 @@ import (
"net/http"
"sync"

"github.com/deluan/navidrome/consts"
"github.com/deluan/navidrome/log"
)

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")
}
4 changes: 2 additions & 2 deletions consts/banner.go
Expand Up @@ -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)
}

Expand Down
2 changes: 0 additions & 2 deletions consts/consts.go
Expand Up @@ -19,8 +19,6 @@ const (
JWTIssuer = "ND"
DefaultSessionTimeout = 30 * time.Minute

UIAssetsLocalPath = "ui/build"

DevInitialUserName = "admin"
DevInitialName = "Dev Admin"

Expand Down
4 changes: 2 additions & 2 deletions engine/cover.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
File renamed without changes.
28 changes: 28 additions & 0 deletions resources/external.go
@@ -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")
}
File renamed without changes
4 changes: 2 additions & 2 deletions server/subsonic/media_retrieval.go
Expand Up @@ -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"
)

Expand All @@ -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")
Expand Down
352 changes: 0 additions & 352 deletions static/bindata.go

This file was deleted.

Binary file removed static/default_cover.jpg
Binary file not shown.

0 comments on commit 1a9663d

Please sign in to comment.