Remove dependency of go-bindata (#818)
* Use new embed functionality for serving UI assets * Use new embed functionality for serving resources. Remove dependency on go-bindata * Remove Go 1.15
This commit is contained in:
+3
-3
@@ -10,12 +10,12 @@ import (
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/httprate"
|
||||
"github.com/go-chi/jwtauth"
|
||||
"github.com/navidrome/navidrome/assets"
|
||||
"github.com/navidrome/navidrome/conf"
|
||||
"github.com/navidrome/navidrome/core/auth"
|
||||
"github.com/navidrome/navidrome/log"
|
||||
"github.com/navidrome/navidrome/model"
|
||||
"github.com/navidrome/navidrome/server/events"
|
||||
"github.com/navidrome/navidrome/ui"
|
||||
)
|
||||
|
||||
type Router struct {
|
||||
@@ -77,8 +77,8 @@ func (app *Router) routes(path string) http.Handler {
|
||||
})
|
||||
|
||||
// Serve UI app assets
|
||||
r.Handle("/", serveIndex(app.ds, assets.AssetFile()))
|
||||
r.Handle("/*", http.StripPrefix(path, http.FileServer(assets.AssetFile())))
|
||||
r.Handle("/", serveIndex(app.ds, ui.Assets()))
|
||||
r.Handle("/*", http.StripPrefix(path, http.FileServer(http.FS(ui.Assets()))))
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package app
|
||||
import (
|
||||
"encoding/json"
|
||||
"html/template"
|
||||
"io/fs"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
@@ -15,7 +16,7 @@ import (
|
||||
)
|
||||
|
||||
// Injects the config in the `index.html` template
|
||||
func serveIndex(ds model.DataStore, fs http.FileSystem) http.HandlerFunc {
|
||||
func serveIndex(ds model.DataStore, fs fs.FS) http.HandlerFunc {
|
||||
policy := bluemonday.UGCPolicy()
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
c, err := ds.User(r.Context()).CountAll()
|
||||
@@ -61,7 +62,7 @@ func serveIndex(ds model.DataStore, fs http.FileSystem) http.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func getIndexTemplate(r *http.Request, fs http.FileSystem) (*template.Template, error) {
|
||||
func getIndexTemplate(r *http.Request, fs fs.FS) (*template.Template, error) {
|
||||
t := template.New("initial state")
|
||||
indexHtml, err := fs.Open("index.html")
|
||||
if err != nil {
|
||||
|
||||
@@ -3,8 +3,8 @@ package app
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
|
||||
@@ -19,7 +19,7 @@ import (
|
||||
var _ = Describe("serveIndex", func() {
|
||||
var ds model.DataStore
|
||||
mockUser := &mockedUserRepo{}
|
||||
fs := http.Dir("tests/fixtures")
|
||||
fs := os.DirFS("tests/fixtures")
|
||||
|
||||
BeforeEach(func() {
|
||||
ds = &tests.MockDataStore{MockedUser: mockUser}
|
||||
|
||||
@@ -31,7 +31,7 @@ var (
|
||||
|
||||
func newTranslationRepository(context.Context) rest.Repository {
|
||||
dir := utils.NewMergeFS(
|
||||
resources.AssetFile(),
|
||||
http.FS(resources.Assets()),
|
||||
http.Dir(filepath.Join(conf.Server.DataFolder, "resources")),
|
||||
)
|
||||
if err := loadTranslations(dir); err != nil {
|
||||
|
||||
@@ -16,7 +16,7 @@ var _ = Describe("Translations", func() {
|
||||
Describe("I18n files", func() {
|
||||
var fs http.FileSystem
|
||||
BeforeEach(func() {
|
||||
fs = resources.AssetFile()
|
||||
fs = http.FS(resources.Assets())
|
||||
})
|
||||
It("contains only valid json language files", func() {
|
||||
dir, _ := fs.Open(consts.I18nFolder)
|
||||
|
||||
@@ -2,6 +2,7 @@ package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -55,12 +56,12 @@ func injectLogger(next http.Handler) http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
func robotsTXT(fs http.FileSystem) func(next http.Handler) http.Handler {
|
||||
func robotsTXT(fs fs.FS) func(next http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if strings.HasSuffix(r.URL.Path, "/robots.txt") {
|
||||
r.URL.Path = "/robots.txt"
|
||||
http.FileServer(fs).ServeHTTP(w, r)
|
||||
http.FileServer(http.FS(fs)).ServeHTTP(w, r)
|
||||
} else {
|
||||
next.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package server
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
@@ -22,7 +23,7 @@ var _ = Describe("middlewares", func() {
|
||||
r := httptest.NewRequest("GET", "/robots.txt", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
robotsTXT(http.Dir("tests/fixtures"))(http.HandlerFunc(next)).ServeHTTP(w, r)
|
||||
robotsTXT(os.DirFS("tests/fixtures"))(http.HandlerFunc(next)).ServeHTTP(w, r)
|
||||
|
||||
Expect(nextCalled).To(BeFalse())
|
||||
Expect(w.Body.String()).To(HavePrefix("User-agent:"))
|
||||
@@ -32,7 +33,7 @@ var _ = Describe("middlewares", func() {
|
||||
r := httptest.NewRequest("GET", "/app/robots.txt", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
robotsTXT(http.Dir("tests/fixtures"))(http.HandlerFunc(next)).ServeHTTP(w, r)
|
||||
robotsTXT(os.DirFS("tests/fixtures"))(http.HandlerFunc(next)).ServeHTTP(w, r)
|
||||
|
||||
Expect(nextCalled).To(BeFalse())
|
||||
Expect(w.Body.String()).To(HavePrefix("User-agent:"))
|
||||
@@ -42,7 +43,7 @@ var _ = Describe("middlewares", func() {
|
||||
r := httptest.NewRequest("GET", "/this_is_not_a_robots.txt_file", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
robotsTXT(http.Dir("tests/fixtures"))(http.HandlerFunc(next)).ServeHTTP(w, r)
|
||||
robotsTXT(os.DirFS("tests/fixtures"))(http.HandlerFunc(next)).ServeHTTP(w, r)
|
||||
|
||||
Expect(nextCalled).To(BeTrue())
|
||||
})
|
||||
|
||||
+2
-2
@@ -7,11 +7,11 @@ import (
|
||||
"github.com/go-chi/chi"
|
||||
"github.com/go-chi/chi/middleware"
|
||||
"github.com/go-chi/cors"
|
||||
"github.com/navidrome/navidrome/assets"
|
||||
"github.com/navidrome/navidrome/conf"
|
||||
"github.com/navidrome/navidrome/consts"
|
||||
"github.com/navidrome/navidrome/log"
|
||||
"github.com/navidrome/navidrome/model"
|
||||
"github.com/navidrome/navidrome/ui"
|
||||
)
|
||||
|
||||
type Handler interface {
|
||||
@@ -59,7 +59,7 @@ func (a *Server) initRoutes() {
|
||||
r.Use(middleware.Compress(5, "application/xml", "application/json", "application/javascript"))
|
||||
r.Use(middleware.Heartbeat("/ping"))
|
||||
r.Use(injectLogger)
|
||||
r.Use(robotsTXT(assets.AssetFile()))
|
||||
r.Use(robotsTXT(ui.Assets()))
|
||||
|
||||
indexHtml := path.Join(conf.Server.BaseURL, consts.URLPathUI, "index.html")
|
||||
r.Get("/*", func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
@@ -46,7 +46,7 @@ func (c *MediaRetrievalController) GetAvatar(w http.ResponseWriter, r *http.Requ
|
||||
}
|
||||
|
||||
func (c *MediaRetrievalController) getPlaceHolderAvatar(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
|
||||
f, err := resources.AssetFile().Open(consts.PlaceholderAvatar)
|
||||
f, err := resources.Assets().Open(consts.PlaceholderAvatar)
|
||||
if err != nil {
|
||||
log.Error(r, "Image not found", err)
|
||||
return nil, newError(responses.ErrorDataNotFound, "Avatar image not found")
|
||||
|
||||
Reference in New Issue
Block a user