forked from golang101/golang101
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgolang101-embed.go
82 lines (71 loc) · 1.86 KB
/
golang101-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
// +build embed
package main
import (
"embed"
"fmt"
"html/template"
"io/fs"
"log"
"net/http"
"os"
"path"
"path/filepath"
"time"
)
//go:embed web articles/*.html
//go:embed articles/res/*.png
//go:embed articles/res/*.jpg
var allFiles embed.FS
var (
staticFiles fs.FS
resFiles fs.FS
)
var staticFilesHandler, resFilesHandler = func() (http.Handler, http.Handler) {
staticFiles, err := fs.Sub(allFiles, path.Join("web", "static"))
if err != nil {
panic(fmt.Sprintf("construct static file system error: %s", err))
}
resFiles, err := fs.Sub(allFiles, path.Join("articles", "res"))
if err != nil {
panic(fmt.Sprintf("construct res file system error: %s", err))
}
//paths1 := printFS("static files", staticFiles)
//paths2 := printFS("res files", resFiles)
//for _, path := range paths1 {
// openFileInFS(staticFiles, path)
//}
//for _, path := range paths2 {
// openFileInFS(resFiles, path)
//}
return http.FileServer(http.FS(staticFiles)), http.FileServer(http.FS(resFiles))
}()
func loadArticleFile(file string) ([]byte, error) {
content, err := allFiles.ReadFile(path.Join("articles", file))
if err != nil {
return nil, err
}
return content, nil
}
func parseTemplate(commonPath string, files ...string) *template.Template {
ts := make([]string, len(files))
for i, f := range files {
ts[i] = path.Join(commonPath, f)
}
return template.Must(template.ParseFS(allFiles, ts...))
}
func updateGolang101() {
if _, err := os.Stat(filepath.Join(".", "golang101.go")); err == nil {
pullGolang101Project("")
return
}
if filepath.Base(os.Args[0]) == "golang101" {
log.Println("go", "install", "go101.org/golang101@latest")
output, err := runShellCommand(time.Minute/2, "", "go", "install", "go101.org/golang101@latest")
if err != nil {
log.Printf("error: %s\n%s", err, output)
} else {
log.Printf("done.")
}
}
// no ideas how to update
}