-
Notifications
You must be signed in to change notification settings - Fork 114
/
static.go
49 lines (41 loc) · 1.05 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
package static
import (
"embed"
"io/fs"
"net/http"
"github.com/gin-gonic/gin"
"github.com/yqchilde/wxbot/engine/pkg/log"
)
type embedFileSystem struct {
http.FileSystem
}
func (e embedFileSystem) Exists(prefix string, path string) bool {
_, err := e.Open(path)
return err == nil
}
func EmbedFolder(fsEmbed embed.FS, targetPath string) ServeFileSystem {
fsys, err := fs.Sub(fsEmbed, targetPath)
if err != nil {
log.Fatalf("static: failed to open embed folder %s: %v", targetPath, err)
}
return embedFileSystem{
FileSystem: http.FS(fsys),
}
}
type ServeFileSystem interface {
http.FileSystem
Exists(prefix string, path string) bool
}
// Serve returns a middleware handler that serves static files in the given directory.
func Serve(urlPrefix string, fs ServeFileSystem) gin.HandlerFunc {
fileServer := http.FileServer(fs)
if urlPrefix != "" {
fileServer = http.StripPrefix(urlPrefix, fileServer)
}
return func(c *gin.Context) {
if fs.Exists(urlPrefix, c.Request.URL.Path) {
fileServer.ServeHTTP(c.Writer, c.Request)
c.Abort()
}
}
}