diff --git a/internal/conf/config.go b/internal/conf/config.go index 67c2dc0fa56..de26e1fe0c4 100644 --- a/internal/conf/config.go +++ b/internal/conf/config.go @@ -49,6 +49,7 @@ type Config struct { Scheme Scheme `json:"scheme"` TempDir string `json:"temp_dir" env:"TEMP_DIR"` BleveDir string `json:"bleve_dir" env:"BLEVE_DIR"` + DistDir string `json:"dist_dir"` Log LogConfig `json:"log"` DelayedStart int `json:"delayed_start" env:"DELAYED_START"` MaxConnections int `json:"max_connections" env:"MAX_CONNECTIONS"` diff --git a/server/static/static.go b/server/static/static.go index 624637936cd..8e2054af7f9 100644 --- a/server/static/static.go +++ b/server/static/static.go @@ -3,25 +3,48 @@ package static import ( "errors" "fmt" + "github.com/alist-org/alist/v3/public" + "io" "io/fs" "net/http" + "os" "strings" "github.com/alist-org/alist/v3/internal/conf" "github.com/alist-org/alist/v3/internal/setting" "github.com/alist-org/alist/v3/pkg/utils" - "github.com/alist-org/alist/v3/public" "github.com/gin-gonic/gin" ) -func InitIndex() { - index, err := public.Public.ReadFile("dist/index.html") +var static fs.FS = public.Public + +func initStatic() { + if conf.Conf.DistDir == "" { + dist, err := fs.Sub(static, "dist") + if err != nil { + utils.Log.Fatalf("failed to read dist dir") + } + static = dist + return + } + static = os.DirFS(conf.Conf.DistDir) +} + +func initIndex() { + indexFile, err := static.Open("index.html") if err != nil { if errors.Is(err, fs.ErrNotExist) { utils.Log.Fatalf("index.html not exist, you may forget to put dist of frontend to public/dist") } utils.Log.Fatalf("failed to read index.html: %v", err) } + defer func() { + _ = indexFile.Close() + }() + index, err := io.ReadAll(indexFile) + if err != nil { + utils.Log.Fatalf("failed to read dist/index.html") + } conf.RawIndexHtml = string(index) siteConfig := getSiteConfig() replaceMap := map[string]string{ @@ -60,7 +83,8 @@ func UpdateIndex() { } func Static(r *gin.RouterGroup, noRoute func(handlers ...gin.HandlerFunc)) { - InitIndex() + initStatic() + initIndex() folders := []string{"assets", "images", "streamer", "static"} r.Use(func(c *gin.Context) { for i := range folders { @@ -70,8 +94,7 @@ func Static(r *gin.RouterGroup, noRoute func(handlers ...gin.HandlerFunc)) { } }) for i, folder := range folders { - folder = "dist/" + folder - sub, err := fs.Sub(public.Public, folder) + sub, err := fs.Sub(static, folder) if err != nil { utils.Log.Fatalf("can't find folder: %s", folder) }