Skip to content

Commit 7352a0f

Browse files
author
蒋吉兆
authored
Merge pull request flipped-aurora#170 from WangLeonard/pack-static-files
打包静态文件到二进制
2 parents e7f9fc0 + 00e61c8 commit 7352a0f

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

server/core/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package core
33
import (
44
"fmt"
55
"gin-vue-admin/global"
6+
_ "gin-vue-admin/packfile"
67
"github.com/fsnotify/fsnotify"
78
"github.com/spf13/viper"
89
)

server/packfile/notUsePackFile.go

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// +build !packfile
2+
3+
package packfile

server/packfile/usePackFile.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// +build packfile
2+
3+
package packfile
4+
5+
import (
6+
"fmt"
7+
"io/ioutil"
8+
"os"
9+
"path/filepath"
10+
"strings"
11+
)
12+
13+
//go:generate go-bindata -o=staticFile.go -pkg=packfile -tags=packfile ../resource/... ../config.yaml
14+
15+
func writeFile(path string, data []byte) {
16+
// 如果文件夹不存在,预先创建文件夹
17+
if lastSeparator := strings.LastIndex(path, "/"); lastSeparator != -1 {
18+
dirPath := path[:lastSeparator]
19+
if _, err := os.Stat(dirPath); err != nil && os.IsNotExist(err) {
20+
os.MkdirAll(dirPath, os.ModePerm)
21+
}
22+
}
23+
24+
// 已存在的文件,不应该覆盖重写,可能在前端更改了配置文件等
25+
if _, err := os.Stat(path); os.IsNotExist(err) {
26+
if err2 := ioutil.WriteFile(path, data, os.ModePerm); err2 != nil {
27+
fmt.Printf("Write file failed: %s\n", path)
28+
}
29+
} else {
30+
fmt.Printf("File exist, skip: %s\n", path)
31+
}
32+
}
33+
34+
func init() {
35+
for key := range _bindata {
36+
filePath, _ := filepath.Abs(strings.TrimPrefix(key, "."))
37+
data, err := Asset(key)
38+
if err != nil {
39+
// Asset was not found.
40+
fmt.Printf("Fail to find: %s\n", filePath)
41+
} else {
42+
writeFile(filePath, data)
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)