Add StaticFSFromEmbed Method

This commit is contained in:
witchc 2021-09-29 01:02:45 +08:00
parent f469c1be39
commit 77e922b97c

View File

@ -5,6 +5,8 @@
package gin package gin
import ( import (
"embed"
"io/fs"
"net/http" "net/http"
"path" "path"
"regexp" "regexp"
@ -172,6 +174,33 @@ func (group *RouterGroup) Static(relativePath, root string) IRoutes {
return group.StaticFS(relativePath, Dir(root, false)) return group.StaticFS(relativePath, Dir(root, false))
} }
// Define redirectable embed fs interface
type fsFunc func(name string) (fs.File, error)
func (f fsFunc) Open(name string) (fs.File, error) {
return f(name)
}
// Redirectable embed files
// use :
// //go:embed static
// var static embed.FS
// ......
// router.StaticFSFromEmbed("/", "static/", static)
func (group *RouterGroup) StaticFSFromEmbed(relativePath, root string, assets embed.FS) IRoutes {
fs := http.FS(fsFunc(func(name string) (fs.File, error) {
assetPath := path.Join(root, name)
// If we can't find the asset, fs can handle the error
file, err := assets.Open(assetPath)
if err != nil {
return nil, err
}
// Otherwise assume this is a legitimate request routed correctly
return file, err
}))
return group.StaticFS(relativePath, fs)
}
// StaticFS works just like `Static()` but a custom `http.FileSystem` can be used instead. // StaticFS works just like `Static()` but a custom `http.FileSystem` can be used instead.
// Gin by default user: gin.Dir() // Gin by default user: gin.Dir()
func (group *RouterGroup) StaticFS(relativePath string, fs http.FileSystem) IRoutes { func (group *RouterGroup) StaticFS(relativePath string, fs http.FileSystem) IRoutes {