From 622674151733e26111f7b91d82771898bd0995c4 Mon Sep 17 00:00:00 2001
From: Deluan <deluan@navidrome.org>
Date: Sat, 3 Feb 2024 12:05:03 -0500
Subject: [PATCH] Create resources.FS only once

---
 resources/banner.go |  7 ++++++-
 resources/embed.go  | 25 +++++++++++--------------
 2 files changed, 17 insertions(+), 15 deletions(-)

diff --git a/resources/banner.go b/resources/banner.go
index 52f3fcd18..0f7f1a549 100644
--- a/resources/banner.go
+++ b/resources/banner.go
@@ -2,6 +2,7 @@ package resources
 
 import (
 	"fmt"
+	"io"
 	"strings"
 	"unicode"
 
@@ -9,7 +10,11 @@ import (
 )
 
 func loadBanner() string {
-	data, _ := Asset("banner.txt")
+	f, err := embedFS.Open("banner.txt")
+	if err != nil {
+		return ""
+	}
+	data, _ := io.ReadAll(f)
 	return strings.TrimRightFunc(string(data), unicode.IsSpace)
 }
 
diff --git a/resources/embed.go b/resources/embed.go
index 860e3130f..5c476e87f 100644
--- a/resources/embed.go
+++ b/resources/embed.go
@@ -2,10 +2,10 @@ package resources
 
 import (
 	"embed"
-	"io"
 	"io/fs"
 	"os"
 	"path"
+	"sync"
 
 	"github.com/navidrome/navidrome/conf"
 	"github.com/navidrome/navidrome/utils"
@@ -13,20 +13,17 @@ import (
 
 var (
 	//go:embed *
-	fsys embed.FS
+	embedFS embed.FS
+	fsOnce  sync.Once
+	fsys    fs.FS
 )
 
 func FS() fs.FS {
-	return utils.MergeFS{
-		Base:    fsys,
-		Overlay: os.DirFS(path.Join(conf.Server.DataFolder, "resources")),
-	}
-}
-
-func Asset(path string) ([]byte, error) {
-	f, err := FS().Open(path)
-	if err != nil {
-		return nil, err
-	}
-	return io.ReadAll(f)
+	fsOnce.Do(func() {
+		fsys = utils.MergeFS{
+			Base:    embedFS,
+			Overlay: os.DirFS(path.Join(conf.Server.DataFolder, "resources")),
+		}
+	})
+	return fsys
 }