-
-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathstatic.go
More file actions
61 lines (53 loc) · 1.58 KB
/
static.go
File metadata and controls
61 lines (53 loc) · 1.58 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
package main
import (
"io/fs"
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
// Since the gin-static middleware uses a version of http.Filesystem with an extra Exists() func, we extend it here.
type httpFS struct {
hfs http.FileSystem // Created by converting fs.FS using http.FS()
fs fs.FS
}
func (f httpFS) Open(name string) (http.File, error) {
return f.hfs.Open("web" + name)
}
func (f httpFS) Exists(prefix string, filepath string) bool {
if p := strings.TrimPrefix(filepath, prefix); len(p) < len(filepath) {
stats, err := fs.Stat(f.fs, "web/"+p)
if err != nil {
return false
}
if stats.IsDir() {
return false
}
return true
}
return false
}
var (
etag = buildTimeUnix
)
// Use unix build time as the ETag for a request, allowing caching of static files.
// Copied from gin-contrib/static:
// https://github.com/gin-gonic/contrib/blob/2b1292699c15c6bc6ee8f0e801a4d0b4e807f366/static/static.go
func serveTaggedStatic(urlPrefix string, fs httpFS) gin.HandlerFunc {
fileserver := http.FileServer(fs)
if urlPrefix != "" {
fileserver = http.StripPrefix(urlPrefix, fileserver)
}
return func(gc *gin.Context) {
if fs.Exists(urlPrefix, gc.Request.URL.Path) {
gc.Header("Cache-Control", "no-cache")
gc.Header("ETag", buildTimeUnix)
ifNoneMatchTag := gc.Request.Header.Get("If-None-Match")
if ifNoneMatchTag != "" && ifNoneMatchTag == etag && (gc.Request.Method == http.MethodGet || gc.Request.Method == http.MethodHead) {
gc.AbortWithStatus(http.StatusNotModified)
} else {
fileserver.ServeHTTP(gc.Writer, gc.Request)
gc.Abort()
}
}
}
}