-
Notifications
You must be signed in to change notification settings - Fork 0
/
gzipstatic.go
153 lines (130 loc) · 3.33 KB
/
gzipstatic.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
// https://github.com/bddjr/gzipstatic-gin
package gzipstatic
import (
"mime"
"net/http"
"path"
"path/filepath"
"regexp"
"strings"
"github.com/gin-gonic/gin"
)
var ExtFillter = regexp.MustCompile(`\.(html|htm|js|json|css)$`)
type EncodeListItem struct {
name string
ext string
}
// Priority from high to low
var EncodeList = []*EncodeListItem{
{
name: "br",
ext: ".br",
}, {
name: "gzip",
ext: ".gz",
},
}
var NoRoute gin.HandlerFunc = nil
// Encoding-By: gzipstatic-gin
var EnableDebugHeader = true
func tryCompress(ctx *gin.Context, name string, fs http.FileSystem) (next bool) {
ae := ctx.GetHeader("Accept-Encoding")
if ae == "" {
return true
}
if strings.HasSuffix(ctx.Request.URL.Path, "/index.html") {
ctx.Header("Location", "./")
ctx.Status(301)
return false
}
if name == "" || strings.HasSuffix(name, "/") {
name += "index.html"
}
ext := ExtFillter.FindString(name)
if ext == "" {
return true
}
for _, encode := range EncodeList {
if !strings.Contains(ae, encode.name) {
continue
}
f, err := fs.Open(name + encode.ext)
if err != nil {
continue
}
defer f.Close()
s, err := f.Stat()
if err != nil || s.IsDir() {
f.Close()
continue
}
h := ctx.Writer.Header()
h.Del("Content-Length")
h.Set("Content-Encoding", encode.name)
h.Set("Content-Type", mime.TypeByExtension(ext))
h.Add("Vary", "Accept-Encoding")
if EnableDebugHeader {
h.Add("Encoding-By", "gzipstatic-gin")
}
http.ServeContent(ctx.Writer, ctx.Request, name, s.ModTime(), f)
return false
}
return true
}
func File(ctx *gin.Context, FilePath string) {
dir, name := filepath.Split(FilePath)
if tryCompress(ctx, name, http.Dir(dir)) {
ctx.File(FilePath)
}
}
func FileFromFS(ctx *gin.Context, name string, fs http.FileSystem) {
if tryCompress(ctx, name, fs) {
ctx.FileFromFS(name, fs)
}
}
func staticFileHandler(group gin.IRoutes, relativePath string, handler gin.HandlerFunc) gin.IRoutes {
if strings.Contains(relativePath, ":") || strings.Contains(relativePath, "*") {
panic("URL parameters can not be used when serving a static file")
}
group.GET(relativePath, handler)
group.HEAD(relativePath, handler)
return group
}
func StaticFile(group gin.IRoutes, relativePath, filepath string) gin.IRoutes {
return staticFileHandler(group, relativePath, func(ctx *gin.Context) {
File(ctx, filepath)
})
}
func StaticFileFS(group gin.IRoutes, relativePath, filepath string, fs http.FileSystem) gin.IRoutes {
return staticFileHandler(group, relativePath, func(ctx *gin.Context) {
FileFromFS(ctx, filepath, fs)
})
}
func Static(group gin.IRoutes, relativePath, root string) gin.IRoutes {
return StaticFS(group, relativePath, gin.Dir(root, false))
}
func StaticFS(group gin.IRoutes, relativePath string, fs http.FileSystem) gin.IRoutes {
if strings.Contains(relativePath, ":") || strings.Contains(relativePath, "*") {
panic("URL parameters can not be used when serving a static folder")
}
handler := func(ctx *gin.Context) {
name := ctx.Param("filepath")
if !tryCompress(ctx, name, fs) {
return
}
if NoRoute != nil {
f, err := fs.Open(name)
if err != nil {
ctx.Status(404)
NoRoute(ctx)
return
}
f.Close()
}
ctx.FileFromFS(name, fs)
}
urlPattern := path.Join(relativePath, "/*filepath")
group.GET(urlPattern, handler)
group.HEAD(urlPattern, handler)
return group
}