-
Notifications
You must be signed in to change notification settings - Fork 309
Expand file tree
/
Copy pathgen_web.go
More file actions
191 lines (173 loc) · 4.87 KB
/
gen_web.go
File metadata and controls
191 lines (173 loc) · 4.87 KB
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
package main
import (
"fmt"
"io/fs"
"log"
"mime"
"os"
"os/exec"
"path/filepath"
"regexp"
"slices"
"sort"
"strings"
)
func main() {
dir, _ := os.Getwd()
if len(os.Args) > 0 {
// Here you can be more cunning, but it will work anyway, for a clean build you need to clean the build folder using the --clean command
if slices.ContainsFunc(os.Args, func(s string) bool {
return s == "--clean" || s == "-c"
}) {
// There are problems with running under windows
if err := run("rm", "-rf", "web/build"); err != nil {
if strings.Contains(err.Error(), "executable file not found") {
// Adding the ability to run on Windows with standard Go commands
if err := os.RemoveAll("web/build"); err != nil {
log.Default().Fatalln(err.Error())
}
} else {
log.Default().Fatalln(err.Error())
}
}
} else {
// Do not uncomment, be aware - its crash the build
//log.Default().Fatalln("Wrong args?")
}
}
if _, err := os.Stat("web/build/static"); os.IsNotExist(err) {
os.Chdir("web")
if err = run("yarn"); err != nil {
log.Default().Fatalln(err.Error())
}
if err = run("yarn", "run", "build"); err != nil {
log.Default().Fatalln(err.Error())
}
os.Chdir(dir)
}
compileHtml := "web/build/"
srcGo := "server/web/pages/"
// There are problems with running under windows
if err := run("rm", "-rf", srcGo+"template/pages"); err != nil {
if strings.Contains(err.Error(), "executable file not found") {
// Adding the ability to run on Windows with standard Go commands
if err = os.RemoveAll(srcGo + "template/pages"); err != nil {
log.Default().Fatalln(err.Error())
}
} else {
log.Default().Fatalln(err.Error())
}
}
// There are problems with running under windows
if err := run("cp", "-r", compileHtml, srcGo+"template/pages/"); err != nil {
if strings.Contains(err.Error(), "executable file not found") {
// Adding the ability to run on Windows with standard Go commands
if err = os.CopyFS(srcGo+"template/pages/", os.DirFS(filepath.Dir(compileHtml))); err != nil {
log.Default().Fatalln(err.Error())
}
} else {
log.Default().Fatalln(err.Error())
}
}
files := make([]string, 0)
filepath.WalkDir(srcGo+"template/pages/", func(path string, d fs.DirEntry, err error) error {
if !d.IsDir() {
name := strings.TrimPrefix(path, srcGo+"template/")
if strings.Contains(name, "\\") {
// Adding the ability to run on Windows with standard Go commands
name = strings.TrimPrefix(strings.ReplaceAll(name, "\\", "/"), "server/web/pages/template/")
}
if !strings.HasPrefix(filepath.Base(name), ".") {
files = append(files, name)
}
}
return nil
})
sort.Strings(files)
fmap := writeEmbed(srcGo+"template/html.go", files)
writeRoute(srcGo+"template/route.go", fmap)
}
func writeEmbed(fname string, files []string) map[string]string {
ff, err := os.Create(fname)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer ff.Close()
embedStr := `package template
import (
_ "embed"
)
`
ret := make(map[string]string)
for _, f := range files {
fname := cleanName(strings.TrimPrefix(f, "pages"))
embedStr += "\n//go:embed " + f + "\nvar " + fname + " []byte\n"
ret[strings.TrimPrefix(f, "pages")] = fname
}
ff.WriteString(embedStr)
return ret
}
func writeRoute(fname string, fmap map[string]string) {
ff, err := os.Create(fname)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer ff.Close()
embedStr := `package template
import (
"crypto/md5"
"fmt"
"github.com/gin-gonic/gin"
)
func RouteWebPages(route gin.IRouter) {
route.GET("/", func(c *gin.Context) {
etag := fmt.Sprintf("%x", md5.Sum(Indexhtml))
c.Header("Cache-Control", "public, max-age=31536000")
c.Header("ETag", etag)
c.Data(200, "text/html; charset=utf-8", Indexhtml)
})
`
mime.AddExtensionType(".map", "application/json")
mime.AddExtensionType(".webmanifest", "application/manifest+json")
// sort fmap
keys := make([]string, 0, len(fmap))
for key := range fmap {
keys = append(keys, key)
}
sort.Strings(keys)
for _, link := range keys {
fmime := mime.TypeByExtension(filepath.Ext(link))
if fmime == "application/xml" || fmime == "application/javascript" {
fmime = fmime + "; charset=utf-8"
}
if fmime == "image/x-icon" {
fmime = "image/vnd.microsoft.icon"
}
embedStr += `
route.GET("` + link + `", func(c *gin.Context) {
etag := fmt.Sprintf("%x", md5.Sum(` + fmap[link] + `))
c.Header("Cache-Control", "public, max-age=31536000")
c.Header("ETag", etag)
c.Data(200, "` + fmime + `", ` + fmap[link] + `)
})
`
}
embedStr += "}\n"
ff.WriteString(embedStr)
}
func run(name string, args ...string) error {
cmd := exec.Command(name, args...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
return cmd.Run()
}
func cleanName(fn string) string {
reg, err := regexp.Compile("[^a-zA-Z0-9]+")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return strings.Title(reg.ReplaceAllString(fn, ""))
}