forked from go101/go101
-
Notifications
You must be signed in to change notification settings - Fork 0
/
go101-embed.go
142 lines (120 loc) · 3.16 KB
/
go101-embed.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
//go:build go1.16
// +build go1.16
package main
import (
"embed"
//"errors"
"fmt"
"html/template"
"io/fs"
"log"
"net/http"
"os"
"path"
"path/filepath"
"time"
)
//go:embed web
//go:embed pages
var allFiles embed.FS
var staticFilesHandler = func() http.Handler {
if wdIsGo101ProjectRoot {
return staticFilesHandler_NonEmbedding
}
staticFiles, err := fs.Sub(allFiles, path.Join("web", "static"))
if err != nil {
panic(fmt.Sprintf("construct static file system error: %s", err))
}
return http.FileServer(http.FS(staticFiles))
}()
func collectPageGroups() map[string]*PageGroup {
if wdIsGo101ProjectRoot {
return collectPageGroups_NonEmbedding()
}
entries, err := fs.ReadDir(allFiles, "pages")
if err != nil {
panic("collect page groups (embedding) error: " + err.Error())
}
pageGroups := make(map[string]*PageGroup, len(entries))
for _, e := range entries {
if e.IsDir() {
group, handler := e.Name(), dummyHandler
resFiles, err := fs.Sub(allFiles, path.Join("pages", e.Name(), "res"))
if err == nil {
var urlGroup string
// For history reason, fundamentals pages uses "/article/xxx" URLs.
if group == "fundamentals" {
urlGroup = "/article"
} else if group != "website" {
urlGroup = "/" + group
}
handler = http.StripPrefix(urlGroup+"/res/", http.FileServer(http.FS(resFiles)))
} else if !os.IsNotExist(err) { // !errors.Is(err, os.ErrNotExist) {
log.Println(err)
}
pageGroups[group] = &PageGroup{resHandler: handler}
}
}
return pageGroups
}
func loadArticleFile(group, file string) ([]byte, error) {
if wdIsGo101ProjectRoot {
return loadArticleFile_NonEmbedding(group, file)
}
content, err := allFiles.ReadFile(path.Join("pages", group, file))
if err != nil {
return nil, err
}
return content, nil
}
func parseTemplate(commonPaths []string, files ...string) *template.Template {
if wdIsGo101ProjectRoot {
return parseTemplate_NonEmbedding(commonPaths, files...)
}
cp := path.Join(commonPaths...)
ts := make([]string, len(files))
for i, f := range files {
ts[i] = path.Join(cp, f)
}
return template.Must(template.ParseFS(allFiles, ts...))
}
func updateGo101() {
if wdIsGo101ProjectRoot {
updateGo101_NonEmbedding()
return
}
if _, err := os.Stat(filepath.Join(".", "go101.go")); err == nil {
pullGo101Project("")
return
}
if filepath.Base(os.Args[0]) == "go101" {
log.Println("go", "install", "go101.org/go101@latest")
output, err := runShellCommand(time.Minute/2, "", "go", "install", "go101.org/go101@latest")
if err != nil {
log.Printf("error: %s\n%s", err, output)
} else {
log.Printf("done.")
}
}
// no ideas how to update
}
//==============================================
//func printFS(title string, s fs.FS) []string {
// println("==================", title)
// files := make([]string, 0, 256)
// fs.WalkDir(s, ".", func(path string, d fs.DirEntry, err error) error {
// println(path)
// files = append(files, path)
// return nil
// })
// return files
//}
//func openFileInFS(s fs.FS, name string) {
// f, err := s.Open(name)
// if err != nil {
// println("open", name, "error:", err.Error())
// return
// }
// f.Close()
// println("open", name, "ok")
//}