代码拉取完成,页面将自动刷新
package AssetHelper
import (
"embed"
"io/fs"
"net/http"
"os"
"path"
)
/**
静态文件打包处理
*/
type fsFunc func(name string) (fs.File, error)
func (f fsFunc) Open(name string) (fs.File, error) {
return f(name)
}
// AssetHandler returns an http.Handler that will serve files from
// the Assets embed.FS. When locating a file, it will strip the given
// prefix from the request and prepend the root to the filesystem.
func AssetHandler(prefix string, assets *embed.FS, root string) http.Handler {
handler := 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
}
//以下禁止访问目录
as,aerr := file.Stat()
if aerr!=nil{
return nil, os.ErrNotExist
}
if as.IsDir(){
return nil,os.ErrNotExist
}
// Otherwise assume this is a legitimate request routed correctly
return file, err
})
return http.StripPrefix(prefix, http.FileServer(http.FS(handler)))
//return http.FileServer(http.FS(handler))
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。