-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstat_get_resource.go
212 lines (181 loc) · 4.91 KB
/
stat_get_resource.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"bytes"
"fmt"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
"strconv"
"github.com/gin-gonic/gin"
)
// ResourceReadDir return `[]fs.DirEntry` from `src`` dir.
func ResourceReadDir(src string) ([]fs.DirEntry, error) {
if config.Debug {
return os.ReadDir(filepath.Join("./", src))
}
return resource.ReadDir(src)
}
// ResourceReadFile return `[]byte` from `src`` file.
func ResourceReadFile(src string) ([]byte, error) {
if config.Debug {
return os.ReadFile(filepath.Join("./", src))
}
return resource.ReadFile(src)
}
var mainCSSBuffer *bytes.Buffer
func getMainCSS(c *gin.Context) {
if mainCSSBuffer == nil {
mainCSSBuffer = new(bytes.Buffer)
}
if 0 == mainCSSBuffer.Len() || config.Debug {
mainCSSBuffer.Reset()
_, err := writeEmbedDir(ResourceReadDir, ResourceReadFile, "res/css", mainCSSBuffer, nil)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
}
c.Status(http.StatusOK)
c.Writer.Header().Set("Content-Type", "text/css; charset=utf-8")
c.Writer.Header().Set("Content-Length", strconv.Itoa(mainCSSBuffer.Len()))
c.Writer.Write(mainCSSBuffer.Bytes())
c.Abort()
}
var appJSBuffer *bytes.Buffer
func getAppJS(c *gin.Context) {
if appJSBuffer == nil {
appJSBuffer = new(bytes.Buffer)
}
if 0 == appJSBuffer.Len() || config.Debug {
appJSBuffer.Reset()
languagesFormat := "// %v, %v \nconst languages = %v"
defaultStringsFormat := "// %v, %v \nconst defaultLang = %v"
_, err := writeEmbedFile(ResourceReadFile, "res/strings/languages.json", appJSBuffer, languagesFormat)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
_, err = writeEmbedFile(ResourceReadFile, "res/strings/strings-en.json", appJSBuffer, defaultStringsFormat)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
_, err = writeEmbedDir(ResourceReadDir, ResourceReadFile, "res/js", appJSBuffer, []string{"res/js/app.js", "res/js/sw.js"})
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
_, err = writeEmbedFile(ResourceReadFile, "res/js/app.js", appJSBuffer)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
}
c.Status(http.StatusOK)
c.Writer.Header().Set("Content-Type", "application/javascript; charset=utf-8")
c.Writer.Header().Set("Content-Length", strconv.Itoa(appJSBuffer.Len()))
c.Writer.Write(appJSBuffer.Bytes())
c.Abort()
}
var themeBuffer *bytes.Buffer
func getDefaultThemes(c *gin.Context) {
if themeBuffer == nil {
themeBuffer = new(bytes.Buffer)
}
if 0 == themeBuffer.Len() || config.Debug {
themeBuffer.Reset()
themeFormat := "// %v \nconst %v = `%v`"
var err error
_, err = writeEmbedDir(ResourceReadDir, ResourceReadFile, "res/theme", themeBuffer, nil, themeFormat)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
}
c.Status(http.StatusOK)
c.Writer.Header().Set("Content-Type", "application/javascript; charset=utf-8")
c.Writer.Header().Set("Content-Length", strconv.Itoa(themeBuffer.Len()))
c.Writer.Write(themeBuffer.Bytes())
c.Abort()
}
var swJsBuffer *bytes.Buffer
func getSWJs(c *gin.Context) {
if swJsBuffer == nil {
swJsBuffer = new(bytes.Buffer)
}
if 0 == swJsBuffer.Len() || config.Debug {
swJsBuffer.Reset()
var err error
_, err = writeEmbedFile(ResourceReadFile, "res/js/sw.js", swJsBuffer)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
}
c.Status(http.StatusOK)
c.Writer.Header().Set("Content-Type", "application/javascript; charset=utf-8")
c.Writer.Header().Set("Content-Length", strconv.Itoa(swJsBuffer.Len()))
c.Writer.Write(swJsBuffer.Bytes())
c.Abort()
}
func writeEmbedDir(
readDir func(src string) ([]fs.DirEntry, error),
readFile func(src string) ([]byte, error),
src string,
dst io.Writer,
ignores []string,
format ...string,
) (int, error) {
themes, err := readDir(src)
if err != nil {
return 0, err
}
count := 0
dir := src + "/"
for _, theme := range themes {
length := 0
path := dir + theme.Name()
skip := false
if nil != ignores {
for _, ignore := range ignores {
if path == ignore {
skip = true
break
}
}
}
if skip {
continue
}
if theme.IsDir() {
length, err = writeEmbedDir(readDir, readFile, path, dst, ignores, format...)
} else {
length, err = writeEmbedFile(readFile, path, dst, format...)
}
count += length
if err != nil {
return count, err
}
}
return count, nil
}
func writeEmbedFile(
readFile func(src string) ([]byte, error),
src string,
dst io.Writer,
format ...string,
) (int, error) {
bs, err := readFile(src)
if err != nil {
return 0, err
}
if 1 == len(format) {
_, fullname := filepath.Split(src)
var extension = filepath.Ext(fullname)
var filename = fullname[0 : len(fullname)-len(extension)]
bs = []byte(fmt.Sprintf(format[0], fullname, filename, string(bs)))
}
return dst.Write(bs)
}