-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine_static.go
More file actions
221 lines (193 loc) · 6.08 KB
/
engine_static.go
File metadata and controls
221 lines (193 loc) · 6.08 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Package gin 提供基于 Gin 的增强上下文与相关组件。
package gin
import (
"context"
"io/fs"
"net/http"
"path"
"sort"
"strings"
staticpkg "github.com/darkit/gin/pkg/static"
)
type staticMount struct {
prefix string
service *staticpkg.Service
}
type staticStopper interface {
Stop()
}
func (e *Engine) needsManagedNoRoute() bool {
return e != nil && (e.regexRouter != nil || len(e.staticMounts) > 0)
}
func (e *Engine) registerStaticMount(prefix string, service *staticpkg.Service) {
if e == nil || service == nil {
return
}
normalized := normalizeStaticMountPrefix(prefix)
for i, mount := range e.staticMounts {
if mount.prefix == normalized {
e.staticMounts[i] = &staticMount{
prefix: normalized,
service: service,
}
e.registerNoRouteIfUnset()
return
}
}
e.staticMounts = append(e.staticMounts, &staticMount{
prefix: normalized,
service: service,
})
sort.SliceStable(e.staticMounts, func(i, j int) bool {
return len(e.staticMounts[i].prefix) > len(e.staticMounts[j].prefix)
})
e.registerNoRouteIfUnset()
}
func (e *Engine) tryServeStaticMounts(c *GinContext) bool {
if e == nil || c == nil || len(e.staticMounts) == 0 {
return false
}
requestPath := requestLookupPath(c.Request)
for _, mount := range e.staticMounts {
subPath, ok := matchStaticMount(mount.prefix, requestPath)
if !ok {
continue
}
return mount.service.TryServePath(c.Writer, c.Request, subPath)
}
return false
}
func (e *Engine) trackStaticStopper(stopper staticStopper) {
if e == nil || stopper == nil {
return
}
e.OnStopped(func(context.Context) error {
stopper.Stop()
return nil
})
}
func normalizeStaticMountPrefix(prefix string) string {
raw := strings.TrimSpace(prefix)
if raw == "" {
return "/"
}
if !strings.HasPrefix(raw, "/") {
raw = "/" + raw
}
cleaned := path.Clean(raw)
if cleaned == "." || cleaned == "/" {
return "/"
}
return strings.TrimSuffix(cleaned, "/")
}
func normalizeStaticRequestPath(requestPath string) string {
if requestPath == "" {
return "/"
}
if !strings.HasPrefix(requestPath, "/") {
requestPath = "/" + requestPath
}
cleaned := path.Clean(requestPath)
if cleaned == "." {
return "/"
}
return cleaned
}
func matchStaticMount(prefix, requestPath string) (string, bool) {
normalizedPrefix := normalizeStaticMountPrefix(prefix)
normalizedRequest := normalizeStaticRequestPath(requestPath)
if normalizedPrefix == "/" {
return normalizedRequest, true
}
if normalizedRequest == normalizedPrefix {
return "/", true
}
if strings.HasPrefix(normalizedRequest, normalizedPrefix+"/") {
subPath := strings.TrimPrefix(normalizedRequest, normalizedPrefix)
if subPath == "" {
subPath = "/"
}
return subPath, true
}
return "", false
}
// Assets 设置基于本地目录的受控静态资源挂载。
func (e *Engine) Assets(relativePath, root string, opts ...staticpkg.Option) IRoutes {
return e.AssetsFS(relativePath, http.Dir(root), opts...)
}
// Site 设置基于本地目录的站点资源挂载。
func (e *Engine) Site(relativePath, root string, opts ...staticpkg.Option) IRoutes {
return e.SiteFS(relativePath, http.Dir(root), opts...)
}
// AssetsFS 设置基于文件系统的受控静态资源挂载。
func (e *Engine) AssetsFS(relativePath string, fileSystem http.FileSystem, opts ...staticpkg.Option) IRoutes {
if e == nil || fileSystem == nil {
return e
}
e.registerStaticMount(relativePath, staticpkg.NewAssetsService(fileSystem, opts...))
return e
}
// SiteFS 设置基于文件系统的站点资源挂载。
func (e *Engine) SiteFS(relativePath string, fileSystem http.FileSystem, opts ...staticpkg.Option) IRoutes {
if e == nil || fileSystem == nil {
return e
}
e.registerStaticMount(relativePath, staticpkg.NewSiteService(fileSystem, opts...))
return e
}
// AssetsZip 设置基于 ZIP 文件的受控静态资源挂载。
func (e *Engine) AssetsZip(relativePath, zipPath string, opts ...staticpkg.Option) error {
config := staticpkg.NewZipFSConfig(zipPath, relativePath, opts...)
fileSystem, err := staticpkg.NewZipFileSystem(config)
if err != nil {
return err
}
e.trackStaticStopper(fileSystem)
e.AssetsFS(relativePath, fileSystem, opts...)
return nil
}
// SiteZip 设置基于 ZIP 文件的站点资源挂载。
func (e *Engine) SiteZip(relativePath, zipPath string, opts ...staticpkg.Option) error {
config := staticpkg.NewZipFSConfig(zipPath, relativePath, opts...)
fileSystem, err := staticpkg.NewZipFileSystem(config)
if err != nil {
return err
}
e.trackStaticStopper(fileSystem)
e.SiteFS(relativePath, fileSystem, opts...)
return nil
}
// AssetsEmbeddedZip 设置基于嵌入式 ZIP 的受控静态资源挂载。
func (e *Engine) AssetsEmbeddedZip(relativePath string, archive fs.FS, archivePath string, opts ...staticpkg.Option) error {
fileSystem, err := staticpkg.NewEmbeddedZipFS(archive, archivePath, opts...)
if err != nil {
return err
}
e.AssetsFS(relativePath, fileSystem, opts...)
return nil
}
// SiteEmbeddedZip 设置基于嵌入式 ZIP 的站点资源挂载。
func (e *Engine) SiteEmbeddedZip(relativePath string, archive fs.FS, archivePath string, opts ...staticpkg.Option) error {
fileSystem, err := staticpkg.NewEmbeddedZipFS(archive, archivePath, opts...)
if err != nil {
return err
}
e.SiteFS(relativePath, fileSystem, opts...)
return nil
}
// FallbackSite 设置全局站点兜底挂载。
func (e *Engine) FallbackSite(root string, opts ...staticpkg.Option) IRoutes {
return e.FallbackSiteFS(http.Dir(root), opts...)
}
// FallbackSiteFS 设置全局站点兜底挂载。
func (e *Engine) FallbackSiteFS(fileSystem http.FileSystem, opts ...staticpkg.Option) IRoutes {
return e.SiteFS("/", fileSystem, opts...)
}
// FallbackSiteZip 设置基于 ZIP 文件的全局站点兜底挂载。
func (e *Engine) FallbackSiteZip(zipPath string, opts ...staticpkg.Option) error {
return e.SiteZip("/", zipPath, opts...)
}
// FallbackSiteEmbeddedZip 设置基于嵌入式 ZIP 的全局站点兜底挂载。
func (e *Engine) FallbackSiteEmbeddedZip(archive fs.FS, archivePath string, opts ...staticpkg.Option) error {
return e.SiteEmbeddedZip("/", archive, archivePath, opts...)
}