Browse Source

Merge pull request #170 from WangLeonard/pack-static-files

打包静态文件到二进制
蒋吉兆 4 years ago
parent
commit
7352a0f769
3 changed files with 49 additions and 0 deletions
  1. 1 0
      server/core/config.go
  2. 3 0
      server/packfile/notUsePackFile.go
  3. 45 0
      server/packfile/usePackFile.go

+ 1 - 0
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"
 )

+ 3 - 0
server/packfile/notUsePackFile.go

@@ -0,0 +1,3 @@
+// +build !packfile
+
+package packfile

+ 45 - 0
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)
+		}
+	}
+}