Files
nps/web/embed.go
T
yisier 62c84206a4 web 静态文件打包进可执行文件:启动时若 web/static、web/views 目录不存在,自动释放内嵌资源,部署不再需要单独拷贝 web 目录
精简发布包:去除打包时冗余的 conf/nps.conf、web 目录
清理失效配置项:移除 appname、runmode、https_default_cert_file、https_default_key_file、https_just_proxy
2026-05-23 14:57:54 -06:00

53 lines
1.1 KiB
Go

package web
import (
"embed"
"io/fs"
"os"
"path/filepath"
"ehang.io/nps/lib/common"
"github.com/astaxie/beego/logs"
)
//go:embed static
var StaticFS embed.FS
//go:embed views
var ViewsFS embed.FS
func ExtractWebFiles(runPath string) {
webDir := filepath.Join(runPath, "web")
staticDir := filepath.Join(webDir, "static")
viewsDir := filepath.Join(webDir, "views")
if !common.FileExists(staticDir) {
extractFS(StaticFS, "static", webDir)
logs.Info("Extracted embedded web/static to", staticDir)
}
if !common.FileExists(viewsDir) {
extractFS(ViewsFS, "views", webDir)
logs.Info("Extracted embedded web/views to", viewsDir)
}
}
func extractFS(efs embed.FS, root string, destDir string) {
fs.WalkDir(efs, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
targetPath := filepath.Join(destDir, path)
if d.IsDir() {
os.MkdirAll(targetPath, 0755)
return nil
}
data, err := efs.ReadFile(path)
if err != nil {
return err
}
os.MkdirAll(filepath.Dir(targetPath), 0755)
os.WriteFile(targetPath, data, 0644)
return nil
})
}