-
Notifications
You must be signed in to change notification settings - Fork 9
/
embed.go
74 lines (62 loc) · 1.37 KB
/
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
package resources
import (
"embed"
"io/fs"
"os"
"path"
"go.uber.org/zap"
)
const (
packagePath = "resources"
staticsPath = "statics"
templatesPath = "templates"
i18nPath = "i18n"
)
var (
//go:embed statics/*
staticsEmbed embed.FS
statics = mustStripPrefix(staticsEmbed, staticsPath)
//go:embed templates/*
templatesEmbed embed.FS
templates = mustStripPrefix(templatesEmbed, templatesPath)
//go:embed i18n/*
i18nEmbed embed.FS
i18n = mustStripPrefix(i18nEmbed, i18nPath)
)
// SetLiveReload make resources load from disk
// if available, instead of from embedded files
func SetLiveReload() {
setLiveReload(&statics, staticsPath)
setLiveReload(&templates, templatesPath)
setLiveReload(&i18n, i18nPath)
}
// GetStatics return statics
func GetStatics() fs.FS {
return statics
}
// GetTemplates return templates
func GetTemplates() fs.FS {
return templates
}
// GetI18n return i18n configs
func GetI18n() fs.FS {
return i18n
}
func mustStripPrefix(sfs fs.FS, prefix string) fs.FS {
dfs, err := fs.Sub(sfs, prefix)
if err != nil {
panic(err)
}
return dfs
}
func setLiveReload(target *fs.FS, name string) {
realPath := path.Join(packagePath, name)
if _, err := os.Stat(realPath); err != nil {
if os.IsNotExist(err) {
// Cannot live reload
return
}
}
zap.S().Infof("live reload %v/*", realPath)
*target = os.DirFS(realPath)
}