-
Notifications
You must be signed in to change notification settings - Fork 0
/
static.go
177 lines (152 loc) · 3.95 KB
/
static.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
package zstatic
import (
"html/template"
"net/http"
"path/filepath"
"strings"
"github.com/sohaha/zlsgo/zfile"
"github.com/sohaha/zlsgo/zlog"
"github.com/sohaha/zlsgo/znet"
"github.com/sohaha/zlsgo/zstring"
"github.com/sohaha/zstatic/build"
)
// mainAssetDirectory stores all the assets
var mainAssetDirectory = build.NewAssetDirectory()
var rootFileGroup *build.FileGroup
var err error
func init() {
rootFileGroup, err = mainAssetDirectory.NewFileGroup(".")
if err != nil {
zlog.Fatal(err)
}
}
// String gets the asset value by name
func String(name string) string {
return rootFileGroup.String(name)
}
// MustString gets the asset value by name
func MustString(name string) (string, error) {
return rootFileGroup.MustString(name)
}
// Bytes gets the asset value by name
func Bytes(name string) []byte {
return rootFileGroup.Bytes(name)
}
// MustBytes gets the asset value by name
func MustBytes(name string) ([]byte, error) {
return rootFileGroup.MustBytes(name)
}
// AddAsset adds the given asset to the root context
func AddAsset(groupName, name, value string) {
fileGroup := mainAssetDirectory.GetGroup(groupName)
if fileGroup == nil {
fileGroup, err = mainAssetDirectory.NewFileGroup(groupName)
if err != nil {
zlog.Fatal(err)
}
}
_ = fileGroup.AddAsset(name, value)
}
// AddByteAsset adds the given asset to the root context
func AddByteAsset(groupName, name string, value []byte) {
fileGroup := mainAssetDirectory.GetGroup(groupName)
if fileGroup == nil {
fileGroup, err = mainAssetDirectory.NewFileGroup(groupName)
if err != nil {
zlog.Fatal(err)
}
}
_ = fileGroup.AddByteAsset(name, value)
}
// Entries returns the file entries as a slice of filenames
func Entries() []string {
return rootFileGroup.Entries()
}
// Reset clears the file entries
func Reset() {
rootFileGroup.Reset()
}
// All All
func All() map[string][]byte {
return rootFileGroup.All()
}
// Group holds a group of assets
func Group(name string) (result *build.FileGroup, err error) {
result = mainAssetDirectory.GetGroup(name)
if result == nil {
result, err = mainAssetDirectory.NewFileGroup(name)
}
return
}
func NewFileserver(dir string, handle ...func(c *znet.Context, name string, content []byte, err error) bool) func(c *znet.Context) {
f, _ := NewFileserverAndGroup(dir, handle...)
return f
}
func NewFileserverAndGroup(dir string, handle ...func(c *znet.Context, name string, content []byte, err error) bool) (func(c *znet.Context), *build.FileGroup) {
const defFile = "index.html"
f, _ := Group(dir)
return func(c *znet.Context) {
name := c.GetParam("file")
nameNil := name == ""
if !nameNil {
name = strings.TrimPrefix(name, "/")
} else if nameNil {
name = c.Request.URL.Path
c.Next()
if c.PrevContent().Code.Load() != int32(404) && c.PrevContent().Code.Load() != int32(0) {
return
}
}
if name == "/" || name == "" {
name = defFile
}
content, err := f.MustBytes(name)
if err != nil && nameNil {
return
}
mime := zfile.GetMimeType(name, content)
c.SetHeader("Content-Type", "")
c.SetHeader("Content-Type", mime)
if len(handle) > 0 && handle[0] != nil {
if handle[0](c, name, content, err) {
return
}
}
if err != nil {
c.String(404, err.Error())
return
}
c.Byte(http.StatusOK, content)
}, f
}
func LoadTemplate(pattern string) (t *template.Template, err error) {
t = template.New("")
var templateData *build.FileGroup
templateData, err = Group(pattern)
if err != nil {
return nil, err
}
all := templateData.All()
if len(all) == 0 {
files, err := filepath.Glob(pattern)
if err != nil {
return nil, err
}
for _, file := range files {
name := filepath.Base(file)
bytes, _ := zfile.ReadFile(file)
t, err = t.New(name).Parse(zstring.Bytes2String(bytes))
if err != nil {
return nil, err
}
}
}
for file := range all {
name := filepath.Base(file)
t, err = t.New(name).Parse(templateData.String(file))
if err != nil {
return nil, err
}
}
return t, nil
}