From 0a18d984ab03b9e3cbc7dacb282617932e96521c Mon Sep 17 00:00:00 2001 From: HFO4 <912394456@qq.com> Date: Tue, 12 Apr 2022 19:11:44 +0800 Subject: [PATCH] Fix: embed static file not work (introduced in #1107) embed file system should be declared in main pkg --- assets | 2 +- bootstrap/init.go | 5 +++-- bootstrap/static.go | 10 ++++------ main.go | 6 +++--- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/assets b/assets index 3d749b7..907f865 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 3d749b7bbe0657b18be8d8d6a66539378de03008 +Subproject commit 907f86550d759afb9b156515f25e60fff5a1d29a diff --git a/bootstrap/init.go b/bootstrap/init.go index 11fa45e..ff55beb 100644 --- a/bootstrap/init.go +++ b/bootstrap/init.go @@ -1,6 +1,7 @@ package bootstrap import ( + "embed" model "github.com/cloudreve/Cloudreve/v3/models" "github.com/cloudreve/Cloudreve/v3/models/scripts" "github.com/cloudreve/Cloudreve/v3/pkg/aria2" @@ -16,7 +17,7 @@ import ( ) // Init 初始化启动 -func Init(path string) { +func Init(path string, statics embed.FS) { InitApplication() conf.Init(path) // Debug 关闭时,切换为生产模式 @@ -79,7 +80,7 @@ func Init(path string) { { "master", func() { - InitStatic() + InitStatic(statics) }, }, { diff --git a/bootstrap/static.go b/bootstrap/static.go index f767f04..82397e8 100644 --- a/bootstrap/static.go +++ b/bootstrap/static.go @@ -19,8 +19,6 @@ import ( const StaticFolder = "statics" -var StaticEmbed embed.FS - type GinFS struct { FS http.FileSystem } @@ -47,13 +45,13 @@ func (b *GinFS) Exists(prefix string, filepath string) bool { } // InitStatic 初始化静态资源文件 -func InitStatic() { +func InitStatic(statics embed.FS) { if util.Exists(util.RelativePath(StaticFolder)) { util.Log().Info("检测到 statics 目录存在,将使用此目录下的静态资源文件") StaticFS = static.LocalFile(util.RelativePath("statics"), false) } else { // 初始化静态资源 - embedFS, err := fs.Sub(StaticEmbed, "assets/build") + embedFS, err := fs.Sub(statics, "assets/build") if err != nil { util.Log().Panic("无法初始化静态资源, %s", err) } @@ -98,9 +96,9 @@ func InitStatic() { } // Eject 抽离内置静态资源 -func Eject() { +func Eject(statics embed.FS) { // 初始化静态资源 - embedFS, err := fs.Sub(StaticEmbed, "assets/build") + embedFS, err := fs.Sub(statics, "assets/build") if err != nil { util.Log().Panic("无法初始化静态资源, %s", err) } diff --git a/main.go b/main.go index 7173183..890a7a1 100644 --- a/main.go +++ b/main.go @@ -17,20 +17,20 @@ var ( ) //go:embed assets/build -var StaticEmbed embed.FS +var staticEmbed embed.FS func init() { flag.StringVar(&confPath, "c", util.RelativePath("conf.ini"), "配置文件路径") flag.BoolVar(&isEject, "eject", false, "导出内置静态资源") flag.StringVar(&scriptName, "database-script", "", "运行内置数据库助手脚本") flag.Parse() - bootstrap.Init(confPath) + bootstrap.Init(confPath, staticEmbed) } func main() { if isEject { // 开始导出内置静态资源文件 - bootstrap.Eject() + bootstrap.Eject(staticEmbed) return }