From 00e61c81dc7404d9bb12ab3126de9188bee7d7d1 Mon Sep 17 00:00:00 2001 From: Leonard Wang Date: Tue, 28 Jul 2020 01:03:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=93=E5=8C=85=E9=9D=99=E6=80=81=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=88=B0=E4=BA=8C=E8=BF=9B=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/core/config.go | 1 + server/packfile/notUsePackFile.go | 3 +++ server/packfile/usePackFile.go | 45 +++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 server/packfile/notUsePackFile.go create mode 100644 server/packfile/usePackFile.go diff --git a/server/core/config.go b/server/core/config.go index 0ff0108b..dcb5b95e 100644 --- a/server/core/config.go +++ b/server/core/config.go @@ -3,6 +3,7 @@ package core import ( "fmt" "gin-vue-admin/global" + _ "gin-vue-admin/packfile" "github.com/fsnotify/fsnotify" "github.com/spf13/viper" ) diff --git a/server/packfile/notUsePackFile.go b/server/packfile/notUsePackFile.go new file mode 100644 index 00000000..e54aad30 --- /dev/null +++ b/server/packfile/notUsePackFile.go @@ -0,0 +1,3 @@ +// +build !packfile + +package packfile diff --git a/server/packfile/usePackFile.go b/server/packfile/usePackFile.go new file mode 100644 index 00000000..055f03dd --- /dev/null +++ b/server/packfile/usePackFile.go @@ -0,0 +1,45 @@ +// +build packfile + +package packfile + +import ( + "fmt" + "io/ioutil" + "os" + "path/filepath" + "strings" +) + +//go:generate go-bindata -o=staticFile.go -pkg=packfile -tags=packfile ../resource/... ../config.yaml + +func writeFile(path string, data []byte) { + // 如果文件夹不存在,预先创建文件夹 + if lastSeparator := strings.LastIndex(path, "/"); lastSeparator != -1 { + dirPath := path[:lastSeparator] + if _, err := os.Stat(dirPath); err != nil && os.IsNotExist(err) { + os.MkdirAll(dirPath, os.ModePerm) + } + } + + // 已存在的文件,不应该覆盖重写,可能在前端更改了配置文件等 + if _, err := os.Stat(path); os.IsNotExist(err) { + if err2 := ioutil.WriteFile(path, data, os.ModePerm); err2 != nil { + fmt.Printf("Write file failed: %s\n", path) + } + } else { + fmt.Printf("File exist, skip: %s\n", path) + } +} + +func init() { + for key := range _bindata { + filePath, _ := filepath.Abs(strings.TrimPrefix(key, ".")) + data, err := Asset(key) + if err != nil { + // Asset was not found. + fmt.Printf("Fail to find: %s\n", filePath) + } else { + writeFile(filePath, data) + } + } +}